2

I'd like to achieve the following results for input in C#. How to do that?

10.000000 -> 10.00
10.200000 -> 10.20
10.254550 -> 10.25455

Thanks in advance.

0

2 Answers 2

5
value.ToString("0.00####################");

Prints exactly what you need for all your sample inputs.

Both 0 and # are part of custom numeric format pattern. You can read what they mean on msdn: Custom Numeric Format Strings.

0
2

Try this

string s = "10.254550";
        Response.Write(Convert.ToDecimal(s).ToString("#.00##"));

# will consider only digits except zero

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.