1

Suppose I have a list of decimal numbers that I must format with a comma every three places, plus the appropriate number of digits after the decimal point. I want to use the .net string.Format method.

I want it to work like this:

string format = ???;
string s1 = string.Format(format, "1500"); // "1,500"
string s2 = string.Format(format, "1500.25"); // "1,500.25"
string s3 = string.Format(format. "3.1415926358979"); // "3.1415926358979"

I have seen other answers where the digits after the decimal are either limited to a fixed number of digits or truncated entirely, but this doesn't work for my application. I want to add the comma-separator to the whole part of the number, but keep the digits after the decimal exactly as they are.

1
  • Seems like you just need en-US CultureInfo Commented Aug 17, 2016 at 19:31

3 Answers 3

2

First problem, you need to parse your strings before you can format them. There maybe some lose of precision. Then you need to decide what your maximum amount of precision you need is. Then you can do something like this:

string format = "{0:#,##0.#############}";
string s1 = string.Format(format, double.Parse("1500")); // "1,500"
string s2 = string.Format(format, double.Parse("1500.25")); // "1,500.25"
string s3 = string.Format(format, double.Parse("3.1415926358979")); // "3.1415926358979"

The # after the decimal place is a place holder for a decimal digit. If there are no more digits it won't show trailing zeros.

If being limited to a number of decimal places or possibly losing precision when converting to double. You could do something really cludgy like this:

public static string DecimalFormatCludge(string original)
{
    var split = original.Split('.');
    return string.Join(".", (new [] { int.Parse(split[0]).ToString("#,##0")}).Concat(split.Skip(1)));
}

This will split on the . in the string, parse the first part as an int, convert it back to a string correctly formatted and then just stick the decimal part back on (if there is one)

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

Comments

0

something like this?

string s1 = format.ToString("#,##0.00");

3 Comments

I'm betting his real problem is that he wants the precision to be the precision of the original number... Actually, if you convert your answer into something like "#,##0.##############################" that might work for him.
That's right, Will, I need to keep the precision of the original number. It won't do to have extra 00s appended after the decimal or drop any digits. It had occurred to me to write #,##0.####################, but it seems like a kludge to me.
@DanielAllenLangdon There are worse kludges, like getting the precision of the number via some log trickery and concatenating your format string. I don't think there's an exact solution in the framework other than this.
0

The format is something like this:

string format = "{0:#,##.##################}";

Comments

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.