如果您想了解如何在JRuby中将浮点数舍入到小数点后2位?的相关知识,那么本文是一篇不可错过的文章,我们将对浮点数如何进行舍入进行全面详尽的解释,并且为您提供关于java–将浮点数舍入为2位小数的最佳
如果您想了解如何在 JRuby 中将浮点数舍入到小数点后 2 位?的相关知识,那么本文是一篇不可错过的文章,我们将对浮点数如何进行舍入进行全面详尽的解释,并且为您提供关于java – 将浮点数舍入为2位小数的最佳做法是什么?、psych::corr.p():如何将 p 值四舍五入到小数点后三位?、Python – 如何将浮点数舍入为1位有效数字、Python将浮点数舍入到最接近的0.05的有价值的信息。
本文目录一览:- 如何在 JRuby 中将浮点数舍入到小数点后 2 位?(浮点数如何进行舍入)
- java – 将浮点数舍入为2位小数的最佳做法是什么?
- psych::corr.p():如何将 p 值四舍五入到小数点后三位?
- Python – 如何将浮点数舍入为1位有效数字
- Python将浮点数舍入到最接近的0.05
如何在 JRuby 中将浮点数舍入到小数点后 2 位?(浮点数如何进行舍入)
如何在 JRuby(1.6.x) 中将浮点数舍入到小数点后 2 位?
number = 1.1164number.round(2)# The above shows the following error# wrong number of arguments (1 for 0)
答案1
小编典典Float#round 可以在 Ruby 1.9 中使用参数,而不是在 Ruby 1.8 中。JRuby 默认为 1.8,但它能够在 1.9
模式下运行。
java – 将浮点数舍入为2位小数的最佳做法是什么?
参见英文答案 > How to round a number to n decimal places in Java 29个
我正在使用eclipse Android SDK.
我需要将浮点值舍入为2位小数.我通常使用数学库使用下一个“技巧”.
float accelerometerX = accelerometerX * 100;
accelerometerX = round(accelerometerX);
Log.d("Test","" + accelerometerX/100);
但我觉得这不是最好的方法.
是否有图书馆可以进行这些类型的操作?
提前致谢.
解决方法:
我在2年前使用Java中的统计数据,我仍然得到一个函数的代码,允许您将数字四舍五入到您想要的小数位数.现在你需要两个,但也许你想用3来比较结果,这个功能给你这个自由.
/**
* Round to certain number of decimals
*
* @param d
* @param decimalPlace
* @return
*/
public static float round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd.floatValue();
}
您需要决定是否要向上或向下舍入.在我的示例代码中,我正在四处寻找.
希望能帮助到你.
编辑
如果你想在它们为零时保留小数位数(我猜它只是为了向用户显示),你只需要将函数类型从float更改为BigDecimal,如下所示:
public static BigDecimal round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd;
}
然后以这种方式调用函数:
float x = 2.3f;
BigDecimal result;
result=round(x,2);
System.out.println(result);
这将打印:
2.30
psych::corr.p():如何将 p 值四舍五入到小数点后三位?
round(corr.p(cormatrix,n = 999)$p,3)
有效
Python – 如何将浮点数舍入为1位有效数字
基本上,0.45应该变为0.4而不是0.5.
并且1945.01应该变为1000.0而不是2000.0.
解决方法
from math import * def roundDown(x,sigfigs=1): #towards -inf exponent = floor(log10(copysign(x,1))) #we don''t want to accidentally try and get an imaginary log (it won''t work anyway) mantissa = x/10**exponent #get full precision mantissa # change floor here to ceil or round to round up or to zero mantissa = floor(mantissa * 10**(sigfigs-1)) / 10**(sigfigs-1) #round mantissa to sigfigs return mantissa * 10**exponent
向零或inf舍入就像将地板改为ceil或round一样容易.以数字方式计算尾数和指数而不是转换为字符串的另一个好处是可以轻松更改sigfigs的数量
Python将浮点数舍入到最接近的0.05
@H_301_6@
我要这个:@H_301_6@
@H_301_6@
>>> my_magical_rounding(1.29,0.05) 1.25 >>> my_magical_rounding(1.30,0.05) 1.30
我可以做这个:@H_301_6@
@H_301_6@
import math def my_magical_rounding(n,r): return n - math.fmod(n,r) >>> my_magical_rounding(1.27,0.05) 1.25 # Yay! >>> my_magical_rounding(1.30,0.05) 1.25 # Boo! I want 1.30.
大概是由于浮点舍入.@H_301_6@
我可以进行一些特殊情况检查,看看n是否足够“接近”r的倍数而不进行减法,这可能会有效,但是有更好的方法吗?@H_301_6@
或者this strategy是我最好的选择?@H_301_6@
解决方法
@H_301_6@
@H_301_6@
def round_down(x,a): return math.floor(x / a) * a
您可以舍入到最接近的倍数,如下所示:@H_301_6@
@H_301_6@
def round_nearest(x,a): return round(x / a) * a
今天关于如何在 JRuby 中将浮点数舍入到小数点后 2 位?和浮点数如何进行舍入的讲解已经结束,谢谢您的阅读,如果想了解更多关于java – 将浮点数舍入为2位小数的最佳做法是什么?、psych::corr.p():如何将 p 值四舍五入到小数点后三位?、Python – 如何将浮点数舍入为1位有效数字、Python将浮点数舍入到最接近的0.05的相关知识,请在本站搜索。
本文标签: