Antoine Jaussoin
Antoine Jaussoin

Reputation: 5172

String formatting: scale and precision from String.Format

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

I have two formatting parameters: scale and precision. I can achieve what I need separately, but it doesn't work with both:

Example (that 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 1,234.568 Millions) As you can see I can't find a format pattern that would both scale and also display decimals.

Any idea?

Upvotes: 7

Views: 1986

Answers (2)

Antoine Jaussoin
Antoine Jaussoin

Reputation: 5172

A colleague of mine got the solution:

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

Upvotes: 9

Daniel A. White
Daniel A. White

Reputation: 190935

I don't think the string format will do this for you. You will have to divide it on your own.

Upvotes: -2

Related Questions