C#float.tryparse为法国文化 [英] C# float.tryparse for French Culture

查看:319
本文介绍了C#float.tryparse为法国文化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入可以包含浮点值范围为:3.06或3,06
中的文化,我们都在为法语,因此当用户输入3.06,我在这个值时,它运行float.tryParse不被转换为3.06到一个新的变量(float类型)

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



我可以简单地做从UI根据输入量的替代。为,,但有通过培养这样做的曼妙/更好的办法?
谢谢

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

推荐答案

您可以使用的超载,需要一个格式提供。您可以通过一个法国文化信息:

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.

这篇关于C#float.tryparse为法国文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆