java 四舍五入使java保留2位小數(shù)示例講解
更新時間:2013年12月02日 12:00:09 作者:
這篇文章主要介紹了java四舍五入使java保留2位小數(shù)示例,大家參考使用
復制代碼 代碼如下:
/*
* 測試四舍五入
*/
package com.icer.test;
/**
*
* @author Hanbin
*/
public class MyRound {
public static void main(String[] args) {
double num = 3.23562;
double number = 0;
number = new MyRound().myRound(num,2);
System.out.println("after:" + number);
}
private double myRound(double number,int index){
double result = 0;
double temp = Math.pow(10, index);
result = Math.round(number*temp)/temp;
return result;
}
}
因為Java中的round函數(shù)在處理完小數(shù)之后就去掉了小數(shù)位,故先乘100,然后再除以100.0。
保證輸出的是小數(shù)。否則會被截成整數(shù)
相關文章
Spring中的FactoryBean實現(xiàn)原理詳解
這篇文章主要介紹了Spring中的FactoryBean實現(xiàn)原理詳解,spring中有兩種類型的Bean,一種是普通的JavaBean,另一種就是工廠Bean(FactoryBean),這兩種Bean都受Spring的IoC容器管理,但它們之間卻有一些區(qū)別,需要的朋友可以參考下2024-02-02
Spring配置中transactionAttributes的使用方法介紹
這篇文章主要介紹了Spring配置中transactionAttributes的使用方法介紹的相關內容,具有一定參考價值,需要的朋友可以了解下。2017-09-09

