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
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.
Comments