Is there a format suitable to display 100000 as $100K
I read that {0, 3} is a way to specify the width of the display number but this only pads the number extra spaces if it is less than 3 digits.
{0:$###K} --displays the full number $100000K
{0, 3} -- displays the full number 100000K
{0, 3:N} --same basic problem 100,000K
Isn't reasonable that one would want to display only the first few digits of a number Or only the last few digits Is there really no format for this

Formatting 100000 to display as $100K
JeffreyCollins
Try "${0:0,}K"
Similarly to display millions use "${0:0,,}M" etc.
DaPoussin
> I tried "${0:0,}K" but the result I got was ${10:0}K. I may be doing something wrong
The following two should give the same result, $100K, so I guess you were doing something wrong.
String.Format("${0:0,}K", 100000)
String.Format("{0:$0,K}", 100000)
> I found that "$#,K" gave (for me) the expected results
This version will give the same result for values >= 500, but will give a different result for smaller values:
String.Format("{0:$0,K}", 499) gives "$0K"
String.Format("{0:$#,K}", 499) gives "$K"
Other variants are possible if you want a thousands separator:
String.Format("{0:$0,K}", 1234000) gives $1234K
String.Format("{0:$#,##0,K}", 1234000) gives $1,234K
And you can show decimals if required:
String.Format("{0:$#,##0,.0K}", 1234500) gives $1,234.5K
linnOz
Why don't you just use your first format and divide your number by 1,000 That way you have absolute control over how the result is rounded.
Mike
Marcello Consolo
Here there is a difference when using the # vs. 0 on the left side of the decimal.
Console.WriteLine(String.Format("
${0:0,}K", 0));Console.WriteLine(String.Format("
${0:#,}K", 0));The latter will print out $K instead of the prefered $0K
foleyp
Michiel de Bruijn
If it was a number to display on a label it would be easier. In this situation my code hands a dataset to a bar chart and the chart renders rounded numbers on the y-axis in increments of 100,000 as seen here.
$300,000-
$200,000-
$100,000-
The chart API won't allow access to those axis values but it will allow me to apply a format string to them.
An option would be to modify the original dataset and divide the row values by 1000 before they are supplied to the chart. Although, I still need to display the unmodified totals on labels below the chart. I guess I could supply to the chart with a modified copy of the dataset and use the original for the labels. I was hoping for a cleaner approach but this will work if needed so thanks.
pellarin
Ronnie,
I found that each of the following work and produce the same results.
Console.WriteLine(String.Format("
${0:0,}K", 100000));Console.WriteLine(String.Format("
{0:$0,K}", 100000));Console.WriteLine(String.Format("
${0:#,}K", 100000));Console.WriteLine(String.Format("
{0:$#,K}", 100000));toolman
Neat! It rounds up too.
Console.WriteLine(String.Format("
${0:0,}K", 499));Console.WriteLine(String.Format("
${0:0,}K", 500));Output:
$0K
$1K
Navaron
JocularJoe - I tried "${0:0,}K" but the result I got was ${10:0}K. I may be doing something wrong.
However, I found that "$#,K" gave (for me) the expected results.
AluminumPork
I don't see the distinction between using 0 or # as the placeholder on the left side of the decimal. However, on the right side of the decimal, # will only be displayed if the decimal value is greater than zero.
Console.WriteLine(String.Format("${0:#,#,#.##}", 100000.00));
Console.WriteLine(String.Format("${0:0,0,0.00}", 100000.00));
Console.WriteLine(String.Format("${0:#,#,#.##}", 100000.02));
Console.WriteLine(String.Format("${0:0,0,0.00}", 100000.02));