0

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!

3
  • 1
    This is a weird request; why are you doing this? Commented Dec 8, 2009 at 17:40
  • A payment provider requires the amounts to be formatted in cents. So it is not allowed to have a seperator. Commented Dec 8, 2009 at 17:56
  • 4
    If you need it formatted in cents, then just multiply by 100 and be done with it. The way your question is worded, it sounds way more general than it needs to be - e.g. it's obvious from what you've said that 1,500.001 should be output as 150000, but your question requires it to be 1500001. Commented Dec 8, 2009 at 17:59

4 Answers 4

7

try:

   decimal d = 1500m;
   string s = (100*d).ToString("0");
Sign up to request clarification or add additional context in comments.

7 Comments

What if there are more decimals after the decimal place?
Do you want the extra decimal values ?
@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, 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: You made me laugh.
|
2

Two solutions:

  • Create your own NumberFormatInfo and CultureInfo and pass it along to ToString.
  • Multiply the number by 100, then use .ToString("0")

4 Comments

I don't think the NumberFormatInfo thing will work. .NET does not allow an empty decimal separator.
Scott P is right; NumberFormatInfo.NumberDecimalSeparator = String.Empty will throw.
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...
Hmm... that's disappointing. I think Pavel is right, it must be due to the need to round-trip the result with Parse().
1

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"

2 Comments

"." should be info.NumberFormat.NumberDecimalSeparator?
Eh, it could be but we know that NumberFormat.NumberDecimalSeparator is . when info is the culture represented by en-US.
0
decimal value = 1500;
Console.WriteLine((value * 100).ToString("0"));

3 Comments

What if there are more decimals after the decimal place?
It wasn't like that before answer changed, it is correct now thank you for the help!
sorry, missed that * 100 in first version; funny thing is one downvote here =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.