java 取小数点后两位 不四舍五入,怎么做

java 取小数点后两位 不四舍五入,怎么做

正常版:
//正常版:
import java.text.DecimalFormat;
import java.math.RoundingMode;

 DecimalFormat formater = new DecimalFormat();

 formater.setMaximumFractionDigits(2);
 formater.setGroupingSize(0);
 formater.setRoundingMode(RoundingMode.FLOOR);

 System.out.println(formater.format(123456.7897456));
简化版:(这个是四色五入,本人亲测.)
// 简化版:(这个是四色五入,本人亲测.)
import java.text.DecimalFormat;

 DecimalFormat formater = new DecimalFormat("#0.##");
 System.out.println(formater.format(123456.7897456));

取自:  http://zhidao.baidu.com/link?url=Wd2GiFvfu_rGGqObUi2HSftJjQhZy9SmCNBr4jrXuezuTB8xk1stNin7JFIc8bIG2ELmaPEU7LGO_lt6SjwcZa

时间: 2024-12-11 09:47:35

java 取小数点后两位 不四舍五入,怎么做的相关文章

Java中取小数点后两位(四种方法)

摘自http://irobot.iteye.com/blog/285537 Java中取小数点后两位(四种方法) 一 Long是长整型,怎么有小数,是double吧     java.text.DecimalFormat   df=new   java.text.DecimalFormat("#.##");     double   d=3.14159;     System.out.println(df.format(d)); 二 java.math.BigDecimal     B

mysql格式化小数保留小数点后两位(小数点格式化)

格式化浮点数的问题,用format(col,2)保留两位小数点,出现一个问题,例如下面的语句,后面我们给出解决方法 SELECT FORMAT(12562.6655,2); 结果:12,562.67 查看文档:Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has

JAVA除法保留小数点后两位的两种方法 Java Math的 floor,round和ceil的总结

floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11. ceil 则是不小于他的最小整数 看例子   Math.floor Math.round Math.ceil 1.4 1 1 2 1.5 1 2 2 1.6 1 2 2 -1.4 -2 -1

Java中double类型的数据精确到小数点后两位

Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); 二: new java.text.DecimalFormat("#.00").format(3.1415926) 三: double d = 3.1415926

用Javascript取float型小数点后两位,例22.127456取成22.13,如何做?

1. 最笨的办法....... [我就怎么干的.........] 1function get()2{3    var s = 22.127456 + "";4    var str = s.substring(0,s.indexOf(".") + 3);5    alert(str);6} 2. 正则表达式效果不错 1<script type="text/javascript">2onload = function(){3    v

【java】java处理随机浮点数(小数点后两位)用RMB的大写数值规则输出

晚上上床前,拿到这个有意思的问题,就想玩弄一番: ============================================================================ 规则:[随机浮点数按照RMB读法写出] 总代码如下: package com.sxd.test; import org.junit.Test; import java.text.DecimalFormat; import java.util.Random; /** * @Author SXD *

Android 保留小数点后两位,并采取四舍五入

//小数点后两位四舍五入 private double formatDouble2(double d) { BigDecimal bigDecimal = new BigDecimal(d); double bg = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); return bg; } 原文地址:https://www.cnblogs.com/niupi/p/11428915.html

java保留小数后两位的四种写法

java保留小数后两位的四种写法 package com.btzh.mis.house.utils; import java.math.BigDecimal;import java.math.RoundingMode;import java.text.DecimalFormat; /** * Double类型数据处理类 * @author weijixiang * @date 2017/10/17. */public class NumberUtil { public static Double

javascript 取小数点后几位四种方法

javascript 取小数点后几位方法总结 Javascript取float型小数点后两位,例22.123456取成22.12,如何做? 1.通过substring截取. function getnum() { var num = 22.123456; var result = num.substring(0,s.indexOf(".")+3); alert(result); } 2. 正则表达式. function getnum() { var num = 22.123456; v