A fixed number of decimal places with no decimal

crthompson

I'm trying to mask a decimal value.

I would like 19.76 to transform into 1976000, or even 1976

Another example would be 500.53 would become 50053000.

I would like to do this with the ToString overloads rather than some kind of conversion method as it suits my xml settings file better.

I wasnt able to find an IFormatProvider that would not assume a decimal point, I suspect thats what I need however.

If I leave out the decimal in the string format like this:

?myvalue
19.76
?((decimal)myvalue).ToString("####000")
"020"

My value is rounded

dee-see

The answer is that you can't do it without converting the string to another format and removing the decimal separator.

There is no basic format that removes the decimal separator. Then you might try the built-in System.Globalization.NumberFormatInfo but it doesn't accept empty separators...

decimal d = 19.76m; 
var format = new System.Globalization.NumberFormatInfo() { NumberDecimalSeparator = string.Empty };
// ArgumentException: Decimal separator cannot be the empty string.
d.ToString(format); 

Now all that's left is to implement IFormatProvider yourself... which will involve manually fiddling with the number to remove the decimal separator (convert to string and replace, multiply to make integer, etc.) anyway!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Round number to decimal places

From Dev

How to format a double with fixed number of significant digits, regardless of the decimal places?

From Dev

Restricting the variables to take a fixed number of decimal places in Matlab

From Dev

How to get number of decimal places

From Java

Show a number to two decimal places

From Dev

Get Number of Decimal Places with Javascript

From Dev

Formatting decimal places with unknown number

From Dev

Rounding number to two decimal places

From Dev

add decimal places to number in a report

From Dev

Rounding number to two decimal places

From Dev

Format number to 2 decimal places only if it has decimal places

From Dev

Print a number to variable number of decimal places

From Dev

Display number with decimal places instead of whole number

From Dev

How to display a number with a given number of decimal places

From Dev

How can I split a string with no delimiters but with fixed number of decimal places - python

From Dev

How can I split a string with no delimiters but with fixed number of decimal places - python

From Dev

Format decimal to number of decimal places without scientific notation

From Dev

Round number to two decimal places and delete any other decimal points

From Dev

Count how many decimal places in a decimal number in bash shell

From Dev

Convert a number to a String with exactly 2 decimal places

From Dev

Specify variable number of decimal places to printf

From Java

Allow 2 decimal places in <input type="number">

From Dev

Format a float with minimum required number of decimal places

From Java

Format number to always show 2 decimal places

From Java

How to round a number to n decimal places in Java

From Dev

Showing a string as number with two decimal places

From Java

Display number always with 2 decimal places in <input>

From Dev

PHP number more than 10 decimal places

From Dev

Force input number decimal places natively

Related Related

  1. 1

    Round number to decimal places

  2. 2

    How to format a double with fixed number of significant digits, regardless of the decimal places?

  3. 3

    Restricting the variables to take a fixed number of decimal places in Matlab

  4. 4

    How to get number of decimal places

  5. 5

    Show a number to two decimal places

  6. 6

    Get Number of Decimal Places with Javascript

  7. 7

    Formatting decimal places with unknown number

  8. 8

    Rounding number to two decimal places

  9. 9

    add decimal places to number in a report

  10. 10

    Rounding number to two decimal places

  11. 11

    Format number to 2 decimal places only if it has decimal places

  12. 12

    Print a number to variable number of decimal places

  13. 13

    Display number with decimal places instead of whole number

  14. 14

    How to display a number with a given number of decimal places

  15. 15

    How can I split a string with no delimiters but with fixed number of decimal places - python

  16. 16

    How can I split a string with no delimiters but with fixed number of decimal places - python

  17. 17

    Format decimal to number of decimal places without scientific notation

  18. 18

    Round number to two decimal places and delete any other decimal points

  19. 19

    Count how many decimal places in a decimal number in bash shell

  20. 20

    Convert a number to a String with exactly 2 decimal places

  21. 21

    Specify variable number of decimal places to printf

  22. 22

    Allow 2 decimal places in <input type="number">

  23. 23

    Format a float with minimum required number of decimal places

  24. 24

    Format number to always show 2 decimal places

  25. 25

    How to round a number to n decimal places in Java

  26. 26

    Showing a string as number with two decimal places

  27. 27

    Display number always with 2 decimal places in <input>

  28. 28

    PHP number more than 10 decimal places

  29. 29

    Force input number decimal places natively

HotTag

Archive