解析浮动的最佳方式? [英] Best way to parse float?

查看:111
本文介绍了解析浮动的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是解析在C#一个浮动的最好方法?
我知道的TryParse,但是我特别想知道的是句点,逗号等。

What is the best way to parse a float in CSharp? I know about TryParse, but what I'm particularly wondering about is dots, commas etc.

我有我的网站的问题。在我的dev的服务器中,','是小数,在'。为分隔符。虽然上督促服务器,它是另一种方式圆。
我怎样才能最好抓住这个?

I'm having problems with my website. On my dev server, the ',' is for decimals, the '.' for separator. On the prod server though, it is the other way round. How can I best capture this?

推荐答案

取决于在输入的来源。

如果您输入来自用户,你应该使用用户/页使用的CultureInfo(<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture.aspx\">Thread.CurrentThread.CurrentUICulture).

If your input comes from the user, you should use the CultureInfo the user/page is using (Thread.CurrentThread.CurrentUICulture).

您可以得到,并通过查看<一个用户的文化标志href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.userlanguages.aspx\">Htt$p$pquest.UserLanguages属性。 (不正确100%,但我发现这是一个非常良好的第一猜测),有了这些信息,您可以设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture.aspx\">Thread.CurrentThread.CurrentUICulture在页面的开始。

You can get and indication of the culture of the user, by looking at the HttpRequest.UserLanguages property. (Not correct 100%, but I've found it a very good first guess) With that information, you can set the Thread.CurrentThread.CurrentUICulture at the start of the page.

如果您的输入来自内部源,你可以使用InvariantCulture解析字符串。

If your input comes from an internal source, you can use the InvariantCulture to parse the string.

解析方法是比较容易使用,如果你的输入来自受控源。也就是说,你已经验证字符串。解析抛出(慢)异常,如果它失败。

The Parse method is somewhat easier to use, if your input is from a controlled source. That is, you have already validated the string. Parse throws a (slow) exception if its fails.

如果输入是不受控制的,(从用户,或其他互联网源)的 的TryParse更好看我。

If the input is uncontrolled, (from the user, or other Internet source) the TryParse looks better to me.

这篇关于解析浮动的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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