Get the currency from current culture?
Use the RegionInfo.ISOCurrencySymbol property. For example: var ri = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID); Console.WriteLine(ri.ISOCurrencySymbol); Output: “USD”
Use the RegionInfo.ISOCurrencySymbol property. For example: var ri = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID); Console.WriteLine(ri.ISOCurrencySymbol); Output: “USD”
While a bit brute-force and not particularly elegant, you could do it like this: public bool TryGetCurrencySymbol(string ISOCurrencySymbol, out string symbol) { symbol = CultureInfo .GetCultures(CultureTypes.AllCultures) .Where(c => !c.IsNeutralCulture) .Select(culture => { try{ return new RegionInfo(culture.Name); } catch { return null; } }) .Where(ri => ri!=null && ri.ISOCurrencySymbol == ISOCurrencySymbol) .Select(ri => ri.CurrencySymbol) .FirstOrDefault(); return … Read more
The easiest answer is number_format(). echo “$ “.number_format($value, 2); If you want your application to be able to work with multiple currencies and locale-aware formatting (1.000,00 for some of us Europeans for example), it becomes a bit more complex. There is money_format() but it doesn’t work on Windows and relies on setlocale(), which is rubbish … Read more
May be you should start with replacing a = (1/b) * c with a = c/b ? It’s not 10x, but still something. If I were you, I’d create my own class Money, which would keep long dollars and long cents, and do math in it.
Do not store money values as float, use the DECIMAL or NUMERIC type: Documentation for MySQL Numeric Types EDIT & clarification: Float values are vulnerable to rounding errors are they have limited precision so unless you do not care that you only get 9.99 instead of 10.00 you should use DECIMAL/NUMERIC as they are fixed … Read more
In addition to mcfinnigan’s answer, you can also use the following to get 2 decimal places ‘%.2f’ % 500 # “500.00” This use case is known as the string format operator
You might want to use the .quantize() method. This will round a decimal value to a certain number of places, the argument you provide specifies the number of places: >>> from decimal import Decimal >>> Decimal(“12.234”).quantize(Decimal(“0.00”)) Decimal(“12.23”) It can also take an argument to specify what rounding approach you want (different accounting systems might want … Read more
First – don’t keep currency in a double – use a decimal instead. Every time. Then use “C0” as the format specifier: decimal numba = 5212.6312M; string s = numba.ToString(“C0”);
I couldn’t find any definitive discussion, so I post my findings, I hope it helps someone. The currency table should include the culture code to make use of any Globalisation Classes. Transactional Method Store in currency local to customer and store multiple conversion rates for the transaction currency that applied when the transaction occurred. Requires … Read more
Try the Currency Format Specifier (“C”). It automatically takes the current UI culture into account and displays currency values accordingly. You can use it with either String.Format or the overloaded ToString method for a numeric type. For example: decimal value = 12345.6789M; // Be sure to use Decimal for money values. Do not use IEEE-754 … Read more