Formatting Numbers with NumberFormat class - Java Language Basics

Java examples for Language Basics:Number Format

Introduction

Here's an example that uses the NumberFormat class to format a double value as currency:

Demo Code

import java.text.NumberFormat;

public class Main {
  public static void main(String[] args) {
    double salesTax = 2.425;
    NumberFormat cf = NumberFormat.getCurrencyInstance();
    System.out.println(cf.format(salesTax));

  }//from  ww  w  .j  a  v a2  s . c  om

}

Related Tutorials