c# - String.Format Currency for K, M and B -
if currency amount large, i'm trying abbreviate it.
for example:
if (amt > 1000000) { decimal d = (decimal)math.round(amt / 1000, 0); return string.format("{0:c0}", d) + " k"; }
if number given on 1 million, take off last 3 digits , replace k. works fine when currency symbol (like $ on left hand side)
however, currency symbols put on right hand side.
so instead of nice looking $100 k usd, i'd 100 € k french euros.
how can change format put k after numbers, , before currency symbol.
seems might step far. ideas?
you can use currencypositivepattern determine if currency symbol comes before or after number. can modify currencysymbol suit needs.
decimal amt = 10000000; thread.currentthread.currentculture = new cultureinfo("fr-fr"); //set france current culture numberformatinfo nfi = cultureinfo.currentculture.numberformat; string currencysymbol = nfi.currencysymbol; int currencyposition = nfi.currencypositivepattern; if (amt > 1000000) { if (currencyposition == 3) // n $ { nfi.currencysymbol = "k " + currencysymbol; } decimal d = (decimal)math.round(amt / 1000, 0); string output = d.tostring("c"); }
i know not best implementation of custom number format, idea across.
Comments
Post a Comment