如何获得浮点数的小数部分? [英] How to get the decimal part of a float?

查看:653
本文介绍了如何获得浮点数的小数部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  float n = 22.65f我需要提取一个浮点数的小数部分,但是我得到奇怪的结果: ; 
//我要x = 0.65f,但是...

x = n%1; // x = 0.6499996

x = n - Math.floor(n); // x = 0.6499996185302734

x = n - (int)n; // x = 0.6499996

为什么会发生这种情况?为什么我得到这些值而不是 0.65

解决方案

float 只有几位数的精确度,所以你应该期望很容易看到一个圆的错误。尝试 double 这有更多的准确性,但仍有舍入误差。你必须舍弃任何回答你有一个理智的输出。

如果这是不希望的,你可以使用BigDecimal没有舍入错误,但有自己的头痛恕我直言。



编辑:你可能会觉得这很有趣。默认的Float.toString()使用最小舍入,但通常是不够的。

  System.out.println(With no四舍五入); 
float n = 22.65f;
System.out.println(n =+ new BigDecimal(n));
浮动预期= 0.65f;
System.out.println(expected =+ new BigDecimal(expected));

System.out.println(n%1 =+ new BigDecimal(n%1));
System.out.println(n - Math.floor(n)=+ new BigDecimal(n - Math.floor(n)));
System.out.println(n - (int)n =+ new BigDecimal(n - (int)n));

System.out.println(With rounding);
System.out.printf(n %% 1 =%.2f%n,n%1);
System.out.printf(n - Math.floor(n)=%.2f%n,n - Math.floor(n));
System.out.printf(n - (int)n =%.2f%n,n - (int)n);

打印

  
n = 22.6499996185302734375
expected = 0.64999997615814208984375 $ b $%1 = 0.6499996185302734375
n - Math.floor(n)= 0.6499996185302734375
n - (int)n = 0.6499996185302734375
四舍五入
n%1 = 0.65
n - Math.floor(n)= 0.65
n - (int)n = 0.65


I need to extract the decimal part of a float number, but I get weird results:

float n = 22.65f;
// I want x = 0.65f, but...

x = n % 1; // x = 0.6499996

x = n - Math.floor(n); // x = 0.6499996185302734

x = n - (int)n; // x = 0.6499996

Why does this happen? Why do I get those values instead of 0.65?

解决方案

float only has a few digit of precision so you should expect to see a round error fairly easily. try double this has more accuracy but still has rounding errors. You have to round any answer you get to have a sane output.

If this is not desireable you can use BigDecimal which does not have rounding errors, but has its own headaches IMHO.

EDIT: You may find this interesting. The default Float.toString() uses minimal rounding, but often its not enough.

System.out.println("With no rounding");
float n = 22.65f;
System.out.println("n= "+new BigDecimal(n));
float expected = 0.65f;
System.out.println("expected= "+new BigDecimal(expected));

System.out.println("n % 1= "+new BigDecimal(n % 1));
System.out.println("n - Math.floor(n) = "+new BigDecimal(n - Math.floor(n)));
System.out.println("n - (int)n= "+new BigDecimal(n - (int)n));

System.out.println("With rounding");
System.out.printf("n %% 1= %.2f%n", n % 1);
System.out.printf("n - Math.floor(n) = %.2f%n", n - Math.floor(n));
System.out.printf("n - (int)n= %.2f%n", n - (int)n);

Prints

With no rounding
n= 22.6499996185302734375
expected= 0.64999997615814208984375
n % 1= 0.6499996185302734375
n - Math.floor(n) = 0.6499996185302734375
n - (int)n= 0.6499996185302734375
With rounding
n % 1= 0.65
n - Math.floor(n) = 0.65
n - (int)n= 0.65

这篇关于如何获得浮点数的小数部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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