将浮点数/双精简舍入到最接近的1/10 [英] rounding a float/double to nearest 1/10th
问题描述
任何人都有一个更好/更简单的方法来将浮点数舍入到最近的
1/10?这是我正在使用的,并且必须有更好的方式,或者可能是我不知道的罐装方法。
double z = atof(arg [1]);
z = z * 100.0;
int zi =(int)floor((double)z);
int ri = zi%10;
zi - = ri;
zi + =(ri< 5)? 0:10;
z =(double)zi /(double)100;
注意:最初双打是浮点数,但1.5 * 100 = 149.9999时
使用花车时,使用双打时给出的正确答案。
任何建议都会非常感谢。
~
Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
Any suggestions would be great thank.
~S
推荐答案
Shea Martin写道:
Shea Martin wrote:
任何人都有一个更好/更简单的方法来舍入浮点数到最近的
1/10?这是我正在使用的,并且必须有更好的方法,或者可能是我不知道的罐装方法。
double z = atof(arg [1]) ;
z = z * 100.0;
int zi =(int)floor((double)z);
int ri = zi%10;
zi - = ri;
zi + =(ri <5)? 0:10;
z =(double)zi /(double)100;
注意:最初双打是浮点数,但是当使用浮点数时1.5 * 100 = 149.9999,使用双打时给出的正确答案。
任何建议都会非常感谢。
~S
Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
Any suggestions would be great thank.
~S
float z2 = atof(arg [1]);
z2 =(浮动)楼层(z2 * 10 + 0.5)/ 10;
我认为这是一个好多了。
~S
float z2 = atof(arg[1]);
z2 = (float)floor(z2*10+0.5)/10;
I thinks this is a lot better.
~S
> float z2 = atof(arg [1]);
> float z2 = atof(arg[1]);
z2 =(float)floor(z2 * 10 + 0.5)/ 10;
z2 = (float)floor(z2*10+0.5)/10;
更好
z2 =(浮动)(楼层(z2 * 10 + 0.5)/ 10)
所以价值被砍成了一个分裂后浮动(如果有的话)。
better
z2 = (float)(floor( z2 * 10 + 0.5 ) / 10)
So the value gets chopped to a float (if at all) after the division.
Shea Martin写道:
Shea Martin wrote:
任何人都有一个更好/更简单的方法来将浮点数舍入到最近的1/10?这是我正在使用的,并且必须有更好的方法,或者可能是我不知道的罐装方法。
double z = atof(arg [1]) ;
z = z * 100.0;
int zi =(int)floor((double)z);
int ri = zi%10;
zi - = ri;
zi + =(ri <5)? 0:10;
z =(double)zi /(double)100;
注意:最初双打是浮点数,但是当使用浮点数时1.5 * 100 = 149.9999,使用双打时给出的正确答案。
Any one have a better/simpler method for rounding a float to the nearest
1/10th? This is currently what I am using, and there must be a better
way, or perhaps a canned method that I am not aware of.
double z = atof(arg[1]);
z = z*100.0;
int zi = (int)floor((double)z);
int ri = zi%10;
zi -= ri;
zi += ( ri < 5 ) ? 0 : 10;
z = (double)zi/(double)100;
NOTE: originally the doubles were floats, but 1.5*100 = 149.9999 when
using floats, correct answer given when doubles used.
z = 0.1 * round(z * 10.0);
z = 0.1 * round( z * 10.0 );
这篇关于将浮点数/双精简舍入到最接近的1/10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!