代码之家  ›  专栏  ›  技术社区  ›  Ricky

DataGridView字段值中的货币符号(不是$)

  •  1
  • Ricky  · 技术社区  · 15 年前

    我正在为危地马拉的一个非营利组织开发一个应用程序。系统中的所有东西都是格查尔,用Q表示(即Q100.00)是100格查尔。

    我需要能够修改任何DataGridView列中的货币值,但是我无法找到一种简单的方法来使用格式,就像使用美元符号一样。我不想使用计算机区域设置,因为使用该系统的某些人使用美国的计算机。

    不确定它是否重要,但值来自sql server数据库的“money”类型字段。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Mark Byers    15 年前

    格式化字符串时,可以指定要使用的区域性:

    decimal cost = 1500m;
    string s = cost.ToString("C", CultureInfo.GetCultureInfo("es-GT"));
    

    结果

    Q1,500.00
    
        2
  •  3
  •   Chris Taylor    15 年前

    如果不想依赖于区域设置,则可以强制应用程序使用瓜塔马拉区域性执行,而不考虑区域设置。

    CultureInfo culture = new CultureInfo("es-GT");
    decimal amount = 123.43M;
    
    // Set the culture for the thread
    System.Threading.Thread.CurrentThread.CurrentCulture = culture;
    
    // Uses the thread culture for formatting
    MessageBox.Show(amount.ToString("c"));
    
    // Alternatively if you do not want to set the thread culture
    // you can explicitly format using the passed culture
    MessageBox.Show(amount.ToString("c", culture));