c#字符串的浮动转换无效? [英] c# string to float conversion invalid?

查看:230
本文介绍了c#字符串的浮动转换无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var x = dr [NationalTotal]。ToString(); 

给我333333333

  var xxx = Convert.ToSingle(dr [NationalTotal]。ToString()); 

给我333333344



解决方案

浮点规范说浮点数的32位表示是


因此,可以表示的整数的最大整数是16777216(0x1000000)。一个简单的测试程序来说服你,这是:

  #include< stdio.h> 

int main(void){
float x;
unsigned long j;
j = 0x00FFFFFC;
int i;
x = j; (i = 0; i <10; i ++)printf(%ld +%d =%f \ n,j,i,x + i)为




$ b $ p $输出:

<$ code> 16777212 + 0 = 16777212.000000
16777212 + 1 = 16777213.000000
16777212 + 2 = 16777214.000000
16777212 + 3 = 16777215.000000
16777212 + 4 = 16777216.000000
16777212 + 5 = 16777216.000000 << <从这里开始,再加一个不能给出正确答案
16777212 + 6 = 16777218.000000
16777212 + 7 = 16777220.000000
16777212 + 8 = 16777220.000000
16777212 + 9 = 16777220.000000

编辑根据问题底下的评论,我们慢慢收敛事实上,你有两个问题,而不是一个。



第一个:为什么会发生这种情况?回答了上述问题。一个浮点数不能完全表示 333333333 ,所以你得到最接近的可表示值,它是 333333344 。第二个问题:我该如何解决这个问题?最初是在评论中回答的,我将在这里重复我的回答:

/ p>

您的数据库浮点数通常不是单精度 - 事实上,默认情况下它是双精度。因此,通过将字符串转换为double,并将其分配给双精度变量来解决问题:

  double xxx = Convert .ToDouble(DR [ NationalTotal]); 

我想引用你的 http://floating-point-gui.de/ - 每个程序员应该知道什么是浮点数。实际上有很多类似的名字指南。基本的阅读,如果你有过使用整数的偏见(大多数人会在他们的编程生涯的某个时刻)。

var x = dr["NationalTotal"].ToString();

gives me 333333333

var xxx = Convert.ToSingle(dr["NationalTotal"].ToString());

gives me 333333344

Any ideas why?

解决方案

The floating point specification says that the 32 bit representation of a floating point number is

Thus, the largest integer that can be represented without loss of (integer) accuracy is 16777216 (0x1000000). A simple test program to convince you that this is so:

#include <stdio.h>

int main(void) {
  float x;
  unsigned long j;
  j = 0x00FFFFFC;
  int i;
  x = j;
  for(i=0; i<10;i++)  printf("%ld + %d = %f\n", j, i, x+i);
}

Output:

16777212 + 0 = 16777212.000000
16777212 + 1 = 16777213.000000
16777212 + 2 = 16777214.000000
16777212 + 3 = 16777215.000000
16777212 + 4 = 16777216.000000
16777212 + 5 = 16777216.000000  <<< from here on, adding one more doesn't give the right answer
16777212 + 6 = 16777218.000000
16777212 + 7 = 16777220.000000
16777212 + 8 = 16777220.000000
16777212 + 9 = 16777220.000000

EDIT Based on the comments underneath the question, we slowly converged on the fact that you had two questions, not one.

The first: "Why is this happening?" is answered with the above. A single float simply is not able to represent 333333333 exactly, so you get the nearest representable value, which is 333333344.

The second: "How do I fix it?" was initially answered by me in the comments - I will reprise my answer here:

Your database floating point number is not typically single precision - in fact, by default it is double precision. Thus, you solve your problem by converting the string to double, and assigning it to a double precision variable:

double xxx = Convert.ToDouble(dr["NationalTotal"]);

I would like to refer you to http://floating-point-gui.de/ - "what every programmer should know about floating point". There are actually many guides out there with similar names. Essential reading if you ever stray from using just integers (and most people will, at some point in their programming career).

这篇关于c#字符串的浮动转换无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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