舍入浮点值 [英] Rounding up a float value

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

问题描述

大家好,


我想帮助将浮动值四舍五入到最接近的整数。

这意味着,


1.2将向上舍入为2

2.15将四舍五入为$

4.01将被四舍五入为5

9.47将被四舍五入等等等等。


我认为使用的函数位于数学中的某处类,但我不知道如何操纵其中可用的方法。


请帮助:)谢谢。

Hi everyone,

I''d like some help to round UP a float value to the nearest higher integer.
This means,

1.2 will be rounded up to 2
2.15 will be rounded up to 3
4.01 will be rounded up to 5
9.47 will be rounded up to 10 etc etc

I think the functions to use lie somewhere in the math class, but i''m not sure how to manipulate the methods available therein.

Please help :) Thank you.

推荐答案

java.math.RoundingMode有你想要的,可能与java.math.BigDecimal一起使用。

Sun的Java API 有你需要的所有方法。
java.math.RoundingMode has what you''re looking for, likely to be used in conjunction with java.math.BigDecimal.

Sun''s Java API has all the methods you''ll need.


你好!


感谢您的回复,但是,任何人都可以提供实际代码来完成以下内容:


浮动x;

x = 3.1417;


//将浮点数x向上舍入为4的语法

x = ......


谢谢fellas。
Hi there!

Thanks for the reply, however, could anyone be kind enough to provide the actual code to complete the following :

float x;
x = 3.1417;

//syntax to round float x upwards to 4
x = ......

Thanks fellas.


BigDecimal太过分了。看一下 java的方法.lang.Math 。如果您阅读文档,则无需要求其他人为您编写代码。
BigDecimal is overkill. Take a look at the methods of java.lang.Math. If you read the documentation, you won''t need to ask for others to write your code for you.


这篇关于舍入浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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