String formatting: scale and precision from String.Format

I need to format numbers (using WPF converters), and the only way I can do this is through string.Format.

I have two formatting options: scale and accuracy. I can achieve what I need separately, but it does not work with both:

Example (works):

string.Format("{0:#,##0,,}", 1234567890.123m) == "1,235" string.Format("{0:#,#.000}", 1234567890.123m) == "1,234,567,890.123" 

What I need:

 string.Format("????", 1234567890.123m) == "1,234.568" 

(which would mean 1234.568 million) As you can see, I can’t find a format template that scales and displays decimal numbers.

Any idea?

+6
source share
2 answers

My colleague got a solution:

 string.Format("{0:#,##0,,.000}", 1234567890.123m) == "1,234.568" 
+8
source

I don't think the string format will do it for you. You will have to split it yourself.

-2
source

Source: https://habr.com/ru/post/921774/


All Articles