1

I have a string series of values which may or may not contain a floating point number. I want to add the thousands separator to this numeric string. I want to have the value with thousands separators and floating point number only when it's there. How can I do this?

Examples:

Input:  23456.78
Output: 23,456.78

Input:  23456
Output: 23,456

1 Answer 1

4

Try parsing to decimal (or double) and then format back to the required representation ("#,#.##########" format string in your case):

String input = "23456.78";

// 23,456.78
String output = decimal
 .Parse(input, CultureInfo.InvariantCulture)
 .ToString("#,#.##########", CultureInfo.InvariantCulture); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, The only problem with your answer is that when i try to input "5754289" and it returns "5754289.00". But i don't need the floating point part with it.
@Dinukshi Jayarathne: if you don't want .00 and alike floating point, then format string should be something like "#,#.##########" (see my edit)

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.