---
title: "JAVA中控制double类型小数点后位数的方法"
author: "fish_bone"
date: "2012-11-21T16:30:14Z"
tags: []
categories: ["编程语言"]
slug: "java-double"
draft: false
lastmod: "2012-11-21T16:30:14Z"
id: 1732225
url: "https://fish-bone.iteye.com/blog/1732225"
---

有时候我们需要控制double类型小数点后的位数，下面简单介绍一下：

假如现在想把某一个double的值限制在小数点后的4位，可以这样做：

01. import java.text.DecimalFormat;
02. **public**   **class**  Main {
03. **public**   **static**   **void**  main(String\[] args) {
04. DecimalFormat df =  **new**  DecimalFormat(  "0.0000 " );
05. **double**  d1 = 1.0;
06. **double**  d2 = 4.56789;
07. System. **out** .println(df.format(d1));
08. System. **out** .println(df.format(d2));
09. }
10. }