简体   繁体   中英

Format Currency string in c# without currency code

I am trying to format a double to currency string in c#

normally, I would use the following code:

using System;
using System.Globalization;
class Demo {
   static void Main() {
      double value = 234.66;
      Console.WriteLine(value.ToString("C", CultureInfo.InvariantCulture));
      Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
   }
}

issue: The first format prepends an unwanted special caracter: ¤234.66 the later one pepends a dollar sign: $234.660

for normal usecases, I could use several culture infos such as in C# formatting currency given currency code (like USD / GBP / FRF)

unfortunately, Crypto currencies are not supported as far as I know of. So I either look for no currency symbol at all (adding it later to the string) or for a custom currency symbol. 在此处输入图像描述

What was quite close was to use balance.ToString("0.##") but in case of 104.10 it would make 104.1 out of it..

var clone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
clone.NumberFormat.CurrencySymbol = "";
var currency = 104.67m;
var stringCurrency = currency.ToString("C", clone);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM