tags:

views:

655

answers:

4

Hello,

I want to create a string from a decimal, whithout the decimal separator;

1,500.00 should become "150000".

What is the proper format for this? (Whithout string.replace , and .)

Thank you!

A: 
decimal value = 1500;
Console.WriteLine((value * 100).ToString("0"));
Rubens Farias
That will return 1500.00 not 150000...
joop
@joop: 1500 * 100 = 150000....
Meta-Knight
What if there are more decimals after the decimal place?
Jason
It wasn't like that before answer changed, it is correct now thank you for the help!
joop
sorry, missed that `* 100` in first version; funny thing is one downvote here =)
Rubens Farias
+4  A: 

try:

   decimal d = 1500m;
   string s = (100*d).ToString("0");
Charles Bretana
What if there are more decimals after the decimal place?
Jason
They won't appear because of the 0 format option.
Meta-Knight
Do you want the extra decimal values ?
Charles Bretana
@Meta-Knight: Right, so what if `d = 1500.001`. The current proposal will output `150000` and not `1500001` which is how I am interpreting the request.
Jason
@Jason, I interpreted it as only get two decimal places... @joop ??
Charles Bretana
@Charles Bretana: That's quite a leap from "I want to create a string from a decimal, [without] the decimal seperator" to "only get two decimal places."
Jason
@Jason, I have strong legs... and didn't you see the way he misspelled "seperator"? That's a clear indication he only wants 2 decimal places... actually, if it matters to you, I got this assumption only from his example - which had two decimal places... but I'm not saying you're wrong... ...only @joop knows.
Charles Bretana
@Charles Bretana: You made me laugh.
Jason
@Jason, Excellent !!
Charles Bretana
+1  A: 

Two solutions:

  • Create your own NumberFormatInfo and CultureInfo and pass it along to ToString.
  • Multiply the number by 100, then use .ToString("0")
richardtallent
I don't think the NumberFormatInfo thing will work. .NET does not allow an empty decimal separator.
Scott P
Scott P is right; `NumberFormatInfo.NumberDecimalSeparator = String.Empty` will throw.
Jason
I suspect it's so because it is used for parsing as well, and it's kinda tricky to find an empty substring when parsing...
Pavel Minaev
Hmm... that's disappointing. I think Pavel is right, it must be due to the need to round-trip the result with Parse().
richardtallent
A: 

What's wrong with String.Replace anyway? It's simple and to the point:

CultureInfo info = CultureInfo.GetCultureInfo("en-US");

decimal m = 1500.00m;
string s = m.ToString("G", info).Replace(".", String.Empty));
Console.WriteLine(s); // outputs "150000"

m = 1500.0m;
string s = m.ToString("G", info).Replace(".", String.Empty));
Console.WriteLine(s); // outputs "15000"

m = 1500.000m;
string s = m.ToString("G", info).Replace(".", String.Empty));
Console.WriteLine(s); // outputs "1500000"


m = 1500.001m;
string s = m.ToString("G", info).Replace(".", String.Empty));
Console.WriteLine(s); // outputs "1500001"

m = 1500.00000000000000000000001m;
string s = m.ToString("G", info).Replace(".", String.Empty));
Console.WriteLine(s); // outputs "150000000000000000000000001"
Jason
`"."` should be `info.NumberFormat.NumberDecimalSeparator`?
Rubens Farias
Eh, it could be but we know that `NumberFormat.NumberDecimalSeparator` is `.` when `info` is the culture represented by `en-US`.
Jason