首页 > 编程知识 正文

java获取小数部分,java中double四舍五入

时间:2023-05-06 19:46:32 阅读:62987 作者:4371

引用来源: https://blog.csdn.net/u 014704879/article/details/41479399/https://blog.csdn.net/shrub er/article/details doubleTPD2=bigd.setscale(2,BigDecimal.ROUND_HALF_UP ).doubleValue ); 结果: 6.15说明:

(new BigDecimal ().setScale ) )方法用于格式化小数点,并具有小数保留模式:

bigdecimalbigd=new bigdecimal (' 3.14159 ); BIGD.setscale(1)表示保留小数一位,缺省情况下,使用四舍五入的BIGD.setscale(1,BigDecimal.ROUND_DOWN )直接删除多馀的小数。 例如,2.35为2.3 bigD.setScale(1) 1,2.35为2.3 bigD.setScale(1,BigDecimal.ROUND_HALF_UP ),2.35为2 round _ ceilingroundingmodetoroundtowardspositiveinfinity .正无限向上舍入round _ downroundingmodetoroundtowardszero.roundingmoward ity.round _ half _ downroundingmodetoroundtowards ' nearest neighbor ' unless boor '向负无限方向舍入idistant,in which case round doward 那样的话,就结束。 例如,1.55表示小数结果为1.5 round _ half _ evenroundingmodetoroundtowardsthe ' nearest neighbor ' unlessbothneighborsareeequidistant, 保留inwhidistant的round towards the even neighbor .会舍入为最接近(距离)的一侧,除非两边(的距离)相等。 否则,如果保留位数为奇数,则使用ROUND_HALF_UP, 偶数时使用round _ half _ down round _ half _ uproundingmodetoroundtowards ' nearest neighbor ' unlessbothneighborsareequidistant 除非两边(的距离)相等,否则向上舍入为最接近(距离)的一方,否则向上舍入,将1.55小数的结果舍入为1.6 round _ unnecessaryroundingmodetoassertthattherequestedoperengthatingmotthed 与hence no rounding is necessary .hence no rounding is necessary .保持的舍入模式round _ uproundingmodetoroundawayfromzero .向远离0的方向移动

1,BigDecimal的初始化赋值方式有几种,但建议使用字符串初始化的方式

1.bigdecimal num1=new bigdecimal (6.285967 ); //这种写法不允许,会导致精度损失2.bigdecimal num2=new bigdecimal (2); //这个写法可以3.bigdecimal num3=new bigdecimal (' 6.285967 ); //一般为方式二:Math.round()

math.round(6.2634675*100 ) * 0.01d; 结果: 6.26说明:

math.round(x )返回参数x四舍五入后的整数近似值,在以下示例中进行说明

1、小数点第一位=5正数: math.round(11.5 )=12负数: math.round(-11.5 )=-112,小数点第一位5正数: math.round (11.46 )=11负数: math

以按照如下方式总结更加容易记忆:1、参数的小数点后第一位<5,运算结果为参数整数部分。2、参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(即正负)不变。3、参数的小数点后第一位=5,正数运算结果为整数部分+1,负数运算结果为整数部分。 总结:大于五全部加,等于五正数加,小于五全不加。Math.round   语法:   Math.round(x);  参数:   x 为一数值。  解释:   方法。返回对参数x四舍五入后所得的整数近似值。roundpublic static long round(double a)返回最接近参数的 long。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为long 类型。换句话说,结果等于以下表达式的值:(long)Math.floor(a + 0.5d)特殊情况如下:如果参数为 NaN,那么结果为 0。如果结果为负无穷大或任何小于等于 Long.MIN_VALUE 的值,那么结果等于Long.MIN_VALUE 的值。如果参数为正无穷大或任何大于等于 Long.MAX_VALUE 的值,那么结果等于Long.MAX_VALUE 的值。参数:a - 舍入为 long 的浮点值。返回:舍入为最接近的 long 值的参数值。roundpublic static int round(float a)返回最接近参数的 int。结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为int 类型。换句话说,结果等于以下表达式的值:(int)Math.floor(a + 0.5f)特殊情况如下:如果参数为 NaN,那么结果为 0。如果结果为负无穷大或任何小于等于 Integer.MIN_VALUE 的值,那么结果等于Integer.MIN_VALUE 的值。如果参数为正无穷大或任何大于等于 Integer.MAX_VALUE 的值,那么结果等于Integer.MAX_VALUE 的值。参数:a - 要舍入为整数的浮点值。返回:舍入为最接近的 int 值的参数值。---------------------4.3<4.4<4.5 so math.round(4.3)=4 math.round(4.4)=4 math.round(4.5)=5-4.6<-4.5 <-4.4 so math.round(-4.6)=-5 math.round(-4.5)=-4math.round(-4.4)=-4-4.51 |-4.50 -4.494.49 | 4.50 4.51因为是负数,所以临界点都是在5的左侧,文字上的“四舍五入”,让人容易糊涂四舍五入都是往右边计算:-----(-5)-----(-4.6)(-4.5)(-4.4)-----(-4)----------(0)----------(4)-----(4.4)(4.5)(4.6)-----(5)----------(-5)<---(-4.6)(-4.5)---------->(-4)----------(0)----------(4)<----------(4.5)(4.6)--->(5)------------------------------------(-4.4)--->(-4)---------(0)-----------(4)<---(4.4)----------------------------注意这些数字的位置关系,正数和负数并不是对称关系,Math.round()的运算时都是由左向右运算,所以:4.5四舍五入应该是取大值为5,-4.5也一样,取大值为-4,因为-4>-4.5>-5

PS:

1,这里说下Math.floor(),其返回值表示小于或等于指定数字的最大整数的数字,如下:

Math.floor( 45.95); // 45 Math.floor( 45.05); // 45 Math.floor( 4 ); // 4 Math.floor(-45.05); // -46 Math.floor(-45.95); // -46

方式三:DecimalFormat

new java.text.DecimalFormat("#.00").format(6.1435829);结果:6.14

解释说明:

DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。它可以支持不同类型的数,包括整数 (123)、定点数 (123.4)、科学记数法表示的数 (1.23E4)、百分数 (12%) 和金额 ($123)这些内容的本地化与区域化,如下例子说明:

DecimalFormat df1 = new DecimalFormat("###,###.0000");//使用系统默认的格式 System.out.println(df1.format(111111123456.12)); 结果:111,111,123,456.1200 Locale.setDefault(Locale.US);//指定区域格式 ,这里使用美国的格式 DecimalFormat df2= new DecimalFormat(); System.out.println(df2.format(111111123456.1200)); 结果:111,111,123,456.12 Locale.setDefault(Locale.FRANCE);//指定区域格式,法国 DecimalFormat df2= new DecimalFormat("###,###.0000");//使用区域格式前提下自定义改造 System.out.println(df2.format(111111123456.12)); 结果:111 111 123 456,1200//----------------------------also use applypattern------------------------------// DecimalFormat df3= new DecimalFormat(); myformat3.applyPattern("##,###.000"); System.out.println(df3.format(11112345.12363)); //这里有四舍五入 结果:11,112,345.124//-----------------控制指数输出-------------------------------------------------// DecimalFormat df4= new DecimalFormat(); myformat4.applyPattern("0.000E0000"); System.out.println(df4.format(10000)); System.out.println(df4.format(12345678.345)); //这里有四舍五入 结果: 1.000E0004 1.235E0007 //------------------百分数的输出-------------------------------------------// DecimalFormat df5= null; try{ df5= (DecimalFormat)NumberFormat.getPercentInstance(); }catch(ClassCastException e){ throw e; } df5.applyPattern("00.0000%"); System.out.println(df5.format(0.34567)); System.out.println(df5.format(1.34567)); 结果:34.5670% 134.5670%

DecimalFormat 包含一组符号,对于各符号的含义解释如下:

0 一个数字 
# 一个数字,不包括 0 
. 小数的分隔符的占位符 
, 分组分隔符的占位符 
; 分隔格式。 
- 缺省负数前缀。 
% 乘以 100 和作为百分比显示 
? 乘以 1000 和作为千进制货币符显示;用货币符号代替;如果双写,用国际货币符号代替。如果出现在一个模式中,用货币十进制分隔符代 替十进制分隔符。 
X 前缀或后缀中使用的任何其它字符,用来引用前缀或后缀中的特殊字符。

PS:

1,DecimalFormat是NumberFormat的一个子类,其实例被指定为特定的地区。因此,你可以使用NumberFormat.getInstance 指定一个地区,然后将结构强制转换为一个DecimalFormat对象。文档中提到这个技术可以在大多情况下适用,但是你需要用try/catch 块包围强制转换以防转换不能正常工作 (大概在非常不明显得情况下使用一个奇异的地区)。
 

方式四:String.format()

/*** %.2f %. 表示 小数点前任意位数 2 表示两位小数 格式后的结果为f 表示浮点型/double tpD = 6.1435628;String result = String.format("%.2f", tpD );结果:6.14

解释说明:

详细看:JAVA字符串格式化-String.format()的使用

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。