ITeye博客

java四舍五入

linjiacheng · 2008-07-24 16:51 · JAVA TEC · Java · UP · Markdown

	private static int getRound(double dSource){
		int iRound;
		BigDecimal deSource = new BigDecimal(dSource);
		iRound= deSource.setScale(0,BigDecimal.ROUND_HALF_UP).intValue();
		return iRound;
	} 

	public static void main(String[] args) { 
		try { 						System.out.println("0.345 : " + getRound(0.345));
			System.out.println("0.545 : " + getRound(0.545));
			System.out.println("0.945 : " + getRound(0.945));
			System.out.println("0.045 : " + getRound(0.045));
		}catch (Exception e) { 
			e.printStackTrace(); 
		} 
	} 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。