See if this would work
$"{Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol}{decimal.Parse("3.12345"):G29}"
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
I am formatting numbers as follows;
@string.Format(new CultureInfo("en-US"), "{0:C5}", (detail.BuyUnitPrice * (detail.CostRatio / 100)) + detail.BuyUnitPrice)
But it adds zeros at the end of the number if it is 4.12 for example. ($4.12000) and of course, if the number is 3.12345, it is displayed like $3.12345
Is there a way to get rid of those trailing zeros and still have the currency sign?
Thanks in advance.
See if this would work
$"{Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol}{decimal.Parse("3.12345"):G29}"
Is there a way to get rid of those trailing zeros and still
have the currency sign?
Have you tried using a custom number format string?
You can use the '0' and '#' symbols to control the
number of zeros.
Decimal Cost = 10M;
Decimal Price = 12345.5M;
Decimal Total = 12345.67800M;
Console.WriteLine(String.Format(new CultureInfo("en-US"),
"{0:$0.00###}", Cost));
Console.WriteLine(String.Format(new CultureInfo("en-US"),
"{0:$0.00###}", Price));
Console.WriteLine(String.Format(new CultureInfo("en-US"),
"{0:$0.00###}", Total));
/*
$10.00
$12345.50
$12345.678
*/
Custom numeric format strings
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
Another example without using a hard coded currency symbol:
CultureInfo ciUS = CultureInfo.CreateSpecificCulture("en-US");
string CurrSymbol = ciUS.NumberFormat.CurrencySymbol;
Console.WriteLine(String.Format(new CultureInfo("en-US"),
"{0}{1:0.00###}", CurrSymbol, Total));
/*
$12345.678
*/
@WayneAKing thank you for your help, this is how I ended.
@switch (detail.Currency)
{
case "USD":
@string.Format(new CultureInfo("en-US"), "{0}{1:0.#####}",new CultureInfo("en-US").NumberFormat.CurrencySymbol, detail.BuyUnitPrice)
break;
case "EURO":
@string.Format(new CultureInfo("en-FR"), "{0}{1:0.#####}",new CultureInfo("en-FR").NumberFormat.CurrencySymbol, detail.BuyUnitPrice)
break;
default:
@string.Format(new CultureInfo("tr-TR"), "{0}{1:0.#####}",new CultureInfo("tr-TR").NumberFormat.CurrencySymbol, detail.BuyUnitPrice)
break;
}