簡體   English   中英

使用特定區域性進行Double.Parse

[英]Double.Parse using specific culture

我在這里嘗試使用加蓬貨幣格式解析數字。

格式使用“。” 用於組分隔,無小數。

這是一個例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            CultureInfo ci = new CultureInfo("fr-FR");

            ci.NumberFormat.CurrencyGroupSeparator = ".";
            ci.NumberFormat.CurrencyDecimalDigits = 0;
            ci.NumberFormat.CurrencySymbol = "CFA";

            Thread.CurrentThread.CurrentCulture = ci;
            Thread.CurrentThread.CurrentUICulture = ci;

            double.Parse("300.000", ci).ToString("C"); 
                    // gives me a FormatException
        }
    }
}

我有什么想念的嗎?

在您的情況下,您需要稍微幫助一下Parse僅僅使用這樣的Parse時,它假設您要獲取一個數字。 法國文化使用,作為小數點分隔符,這就是您的代碼引發異常的原因。

試試這個,代替:

double.Parse("300.000", NumberStyles.Currency, ci).ToString("C");

現在,將遵循您在ci文化中指定的貨幣規則,將字符串正確地解析為貨幣。

而且-正如其他人所說,在處理貨幣時,您應該真正使用decimal Double根本不夠精確

添加此:ci.NumberFormat.NumberGroupSeparator =“。”;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM