java代码保留两位小数的做法
在日常Java开发中,少不了会有页面或界面上展示的数据保留小数点后两位的需求,这里列举了几种可行的方法(有更好的方法可以分享给我)
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Formatter;
/**页面或界面上展示的数据保留小数点后两位。
*/
public class DecimalDigits {
/**使用DecimalFormat,保留小数点后两位
*/
public static String format1(double value){
DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(value);
}
/**使用BigDecimal,保留小数点后两位
*/
public static String format2(double value) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.HALF_UP);
return bd.toString();
}
/**使用NumberFormat,保留小数点后两位
* setMinimumFractionDigits设置成2
* 如果不这么做,那么当value的值是100.00的时候返回100
* 而不是100.00
*/
public static String format3(double value) {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
nf.setRoundingMode(RoundingMode.HALF_UP);
//如果想输出的格式用逗号隔开,可以设置成true
nf.setGroupingUsed(false);
return nf.format(value);
}
/**使用java.util.Formatter,保留小数点后两位
* %.2f % 表示 小数点前任意位数 2 表示两位小数 格式后的结果为 f 表示浮点型
*/
public static String format4(double value) {
return new Formatter().format("%.2f", value).toString();
}
/**使用String.format来实现。
* 这个方法其实和format4是一样的
*/
public static String format5(double value) {
return String.format("%.2f", value).toString();
}
public static void main(String[] args) {
System.out.println("format1:"+format1(1));
System.out.println("format2:"+format1(1));
System.out.println("format3:"+format1(1));
System.out.println("format4:"+format1(1));
System.out.println("format5:"+format1(1));
}
}
运行结果:
D:\Java\jdk1.8.0_281\bin\java.exe "-javaagent:E:\tools\JetBrains\IntelliJ IDEA 2022.2.3\lib\idea_rt.jar=61221:E:\tools\JetBrains\IntelliJ IDEA 2022.2.3\bin" -Dfile.encoding=UTF-8 -classpath D:\Java\jdk1.8.0_281\jre\lib\charsets.jar;D:\Java\jdk1.8.0_281\jre\lib\deploy.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_281\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_281\jre\lib\javaws.jar;D:\Java\jdk1.8.0_281\jre\lib\jce.jar;D:\Java\jdk1.8.0_281\jre\lib\jfr.jar;D:\Java\jdk1.8.0_281\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_281\jre\lib\jsse.jar;D:\Java\jdk1.8.0_281\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_281\jre\lib\plugin.jar;D:\Java\jdk1.8.0_281\jre\lib\resources.jar;D:\Java\jdk1.8.0_281\jre\lib\rt.jar;D:\IDEACODE\store\target\test-classes;D:\IDEACODE\store\target\classes;E:\java\Repository\org\springframework\boot\spring-boot-starter-web\2.7.2\spring-boot-starter-web-2.7.2.jar;E:\java\Repository\org\springframework\boot\spring-boot-starter\2.7.2\spring-boot-starter-2.7.2.jar;E:\java\Repository\org\springframework\boot\spring-boot\2.7.2\spring-boot-2.7.2.jar;E:\java\Repository\org\springframework\boot\spring-boot-autoconfigure\2.7.2\spring-boot-autoconfigure-2.7.2.jar;E:\java\Repository\org\springframework\boot\spring-boot-starter-logging\2.7.2\spring-boot-starter-logging-2.7.2.jar;E:\java\Repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;E:\java\Repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;E:\java\Repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;E:\java\Repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;E:\java\Repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;E:\java\Repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\java\Repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;E:\java\Repository\org\springframework\boot\spring-boot-starter-json\2.7.2\spring-boot-starter-json-2.7.2.jar;E:\java\Repository\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;E:\java\Repository\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;E:\java\Repository\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;E:\java\Repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;E:\java\Repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;E:\java\Repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;E:\java\Repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.2\spring-boot-starter-tomcat-2.7.2.jar;E:\java\Repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;E:\java\Repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;E:\java\Repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;E:\java\Repository\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;E:\java\Repository\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;E:\java\Repository\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;E:\java\Repository\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;E:\java\Repository\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;E:\java\Repository\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;E:\java\Repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\2.2.2\mybatis-spring-boot-starter-2.2.2.jar;E:\java\Repository\org\springframework\boot\spring-boot-starter-jdbc\2.7.2\spring-boot-starter-jdbc-2.7.2.jar;E:\java\Repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;E:\java\Repository\org\springframework\spring-jdbc\5.3.22\spring-jdbc-5.3.22.jar;E:\java\Repository\org\springframework\spring-tx\5.3.22\spring-tx-5.3.22.jar;E:\java\Repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.2.2\mybatis-spring-boot-autoconfigure-2.2.2.jar;E:\java\Repository\org\mybatis\mybatis\3.5.9\mybatis-3.5.9.jar;E:\java\Repository\org\mybatis\mybatis-spring\2.0.7\mybatis-spring-2.0.7.jar;E:\java\Repository\mysql\mysql-connector-java\8.0.29\mysql-connector-java-8.0.29.jar;E:\java\Repository\org\springframework\boot\spring-boot-starter-test\2.7.2\spring-boot-starter-test-2.7.2.jar;E:\java\Repository\org\springframework\boot\spring-boot-test\2.7.2\spring-boot-test-2.7.2.jar;E:\java\Repository\org\springframework\boot\spring-boot-test-autoconfigure\2.7.2\spring-boot-test-autoconfigure-2.7.2.jar;E:\java\Repository\com\jayway\jsonpath\json-path\2.7.0\json-path-2.7.0.jar;E:\java\Repository\net\minidev\json-smart\2.4.8\json-smart-2.4.8.jar;E:\java\Repository\net\minidev\accessors-smart\2.4.8\accessors-smart-2.4.8.jar;E:\java\Repository\org\ow2\asm\asm\9.1\asm-9.1.jar;E:\java\Repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;E:\java\Repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;E:\java\Repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;E:\java\Repository\org\assertj\assertj-core\3.22.0\assertj-core-3.22.0.jar;E:\java\Repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;E:\java\Repository\org\junit\jupiter\junit-jupiter\5.8.2\junit-jupiter-5.8.2.jar;E:\java\Repository\org\junit\jupiter\junit-jupiter-api\5.8.2\junit-jupiter-api-5.8.2.jar;E:\java\Repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;E:\java\Repository\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;E:\java\Repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;E:\java\Repository\org\junit\jupiter\junit-jupiter-params\5.8.2\junit-jupiter-params-5.8.2.jar;E:\java\Repository\org\junit\jupiter\junit-jupiter-engine\5.8.2\junit-jupiter-engine-5.8.2.jar;E:\java\Repository\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;E:\java\Repository\org\mockito\mockito-core\4.5.1\mockito-core-4.5.1.jar;E:\java\Repository\net\bytebuddy\byte-buddy\1.12.12\byte-buddy-1.12.12.jar;E:\java\Repository\net\bytebuddy\byte-buddy-agent\1.12.12\byte-buddy-agent-1.12.12.jar;E:\java\Repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;E:\java\Repository\org\mockito\mockito-junit-jupiter\4.5.1\mockito-junit-jupiter-4.5.1.jar;E:\java\Repository\org\skyscreamer\jsonassert\1.5.1\jsonassert-1.5.1.jar;E:\java\Repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;E:\java\Repository\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;E:\java\Repository\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar;E:\java\Repository\org\springframework\spring-test\5.3.22\spring-test-5.3.22.jar;E:\java\Repository\org\xmlunit\xmlunit-core\2.9.0\xmlunit-core-2.9.0.jar;E:\java\Repository\junit\junit\4.13.2\junit-4.13.2.jar;E:\java\Repository\org\hamcrest\hamcrest-core\2.2\hamcrest-core-2.2.jar flash.DecimalDigits
format1:1.00
format2:1.00
format3:1.00
format4:1.00
format5:1.00
Process finished with exit code 0