C# float.tryparse 用于法国文化

发布于 2024-08-30 10:15:14 字数 357 浏览 1 评论 0原文

我有一个用户输入,其中可以包含浮点值,范围为:3.06 OR 3,06 我们所处的文化是法语,因此当用户输入 3.06 并且我对该值运行 float.tryParse 时,它​​不会转换为 3.06 到新变量(类型 float)中,

// inputUsedAmount.Value from UI is : 3.06
float usedAmount = 0.0f;
float.TryParse(inputUsedAmount.Value, out usedAmount);
// returns false

我可以简单地对输入的金额进行替换用户界面来自“.”到“,”,但是有没有一种优雅/更好的方式通过文化来做到这一点? 谢谢

I have a user input which can contain float values ranging from : 3.06 OR 3,06
The culture we are in is French and thus when the user inputs 3.06 and I run a float.tryParse over this value it does not get converted to 3.06 into a new variable (type float)

// inputUsedAmount.Value from UI is : 3.06
float usedAmount = 0.0f;
float.TryParse(inputUsedAmount.Value, out usedAmount);
// returns false

I can simply do a replace on the amount entered from UI from "." to ",", but is there a graceful/better way of doing this through Culture ?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热鲨 2024-09-06 10:15:15

您可以使用采用格式提供程序的重载。您可以通过一个法国文化信息:

string value;
NumberStyles style;
CultureInfo culture;
double number;

value = "1345,978";
style = NumberStyles.AllowDecimalPoint;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Converted '1345,978' to 1345.978.

You can use the overload that takes a format provider. You can pass through a French culture info:

string value;
NumberStyles style;
CultureInfo culture;
double number;

value = "1345,978";
style = NumberStyles.AllowDecimalPoint;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Converted '1345,978' to 1345.978.
伴梦长久 2024-09-06 10:15:15
float usedAmount;

// try parsing with "fr-FR" first
bool success = float.TryParse(inputUsedAmount.Value,
                              NumberStyles.Float | NumberStyles.AllowThousands,
                              CultureInfo.GetCultureInfo("fr-FR"),
                              out usedAmount);

if (!success)
{
    // parsing with "fr-FR" failed so try parsing with InvariantCulture
    success = float.TryParse(inputUsedAmount.Value,
                             NumberStyles.Float | NumberStyles.AllowThousands,
                             CultureInfo.InvariantCulture,
                             out usedAmount);
}

if (!success)
{
    // parsing failed with both "fr-FR" and InvariantCulture
}
float usedAmount;

// try parsing with "fr-FR" first
bool success = float.TryParse(inputUsedAmount.Value,
                              NumberStyles.Float | NumberStyles.AllowThousands,
                              CultureInfo.GetCultureInfo("fr-FR"),
                              out usedAmount);

if (!success)
{
    // parsing with "fr-FR" failed so try parsing with InvariantCulture
    success = float.TryParse(inputUsedAmount.Value,
                             NumberStyles.Float | NumberStyles.AllowThousands,
                             CultureInfo.InvariantCulture,
                             out usedAmount);
}

if (!success)
{
    // parsing failed with both "fr-FR" and InvariantCulture
}
三生殊途 2024-09-06 10:15:15

您可以在 TryParse 方法中传递区域性:

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out float result);

You can pass culture inside TryParse method:

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out float result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文