Converting String To Float in C#(在 C# 中将字符串转换为浮点数)
问题描述
我正在转换像41.00027357629127"这样的字符串,我正在使用;
I am converting a string like "41.00027357629127", and I am using;
Convert.ToSingle("41.00027357629127");
或
float.Parse("41.00027357629127");
这些方法返回 4.10002732E+15.
当我转换为浮点数时,我想要41.00027357629127".这个字符串应该是一样的...
When I convert to float I want "41.00027357629127". This string should be the same...
推荐答案
您的线程的语言环境设置为小数点为,"而不是.".
Your thread's locale is set to one in which the decimal mark is "," instead of ".".
试试这个:
float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
但是请注意,浮点数不能容纳那么多位的精度.您必须使用 double 或 Decimal 才能这样做.
Note, however, that a float cannot hold that many digits of precision. You would have to use double or Decimal to do so.
这篇关于在 C# 中将字符串转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# 中将字符串转换为浮点数


基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01