集册 Java实例教程 格式化双精度和长十进制值

格式化双精度和长十进制值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

629
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用DecimalFormat类格式化该值并将其舍入为精度。

import java.text.DecimalFormat;

import java.text.NumberFormat;//N  o w  J a v a . c o m

import java.util.Locale;


public class Main {


  public static void main(String[] args) {

    formatDouble(new Double("345.9372"));

  }


  public static void formatDouble(double myDouble) {

    NumberFormat numberFormatter = new DecimalFormat("##.000");

    String result = numberFormatter.format(myDouble);


    System.out.println(result);


    // Obtains an instance of NumberFormat class

    NumberFormat format = NumberFormat.getInstance();


    // Format a double value for the current locale

    String result2 = format.format(83.404);

    System.out.println("Current Locale: " + result2);
    /**
     from
    * 时 代 J a v a 公 众 号 - nowjava.com 
    **/


    // Format a double value for an Italian locale

    result = NumberFormat.getInstance(Locale.ITALIAN).format(83.404);

    System.out.println("Italian Locale: " + result);


    // Parse a String into a Number

    
展开阅读全文