0

I want to format a string with 1000's separator and also two decimal places by default

I tried this one but it's not working as it fails to append two zeros when there are no decimals

String.Format("{0:#,##0.##}", money); //I want something like 1000.23 and also 1000.00
1
  • You've already used 0 as a placeholder in your string. Why don't you use it for decimals too? Commented Oct 10, 2016 at 11:28

1 Answer 1

2

Just to be sure you're not making this more complicated then necessary

String.Format("{0:f2}", money);

Maybe standard number format is enough and custom number format is not needed. But as mentioned if your Culture has a , as seperator, this won't work.

Sign up to request clarification or add additional context in comments.

6 Comments

You should also include CultureInfo in the call to be sure.
@kiziu which CultureInfo? The application's locale will be used by default. Typically, that's the correct option when formatting for display. InvariantCulture is used when the string is used for storage or communicating with other programs
@PanagiotisKanavos, The one that will correctly present the value according to the requirements. Nowhere does the OP say if the application locale will format the value correctly. That is why I did not specify which CultureInfo, only that one should be provided to ensure correct formatting.
I wnat to have a comma after 1000's also, how should I do it?
Go string interpolation! $"{money:n2}"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.