Home Java Java-get-example Java get decimal value



Java get decimal value
Posted on: October 24, 2008 at 12:00 AM
We can also get the decimal value in the specified format as we wish by calling DecimalFormat class. It will format a long value to the specified string pattern.

Java get decimal value

     

We can also get the decimal value in the specified format as we wish by calling DecimalFormat class. It will format a long value to the specified string pattern.

DecimalFormat dec = new DecimalFormat("###.##"); 
System.out.println(dec.format(12345.6789)); 

Above lines of code prints decimal value 12345.6789 in the 12345.68 format. Also we can print this decimal value in other different formats by defining the decimal format pattern. Here is the full example code for GetDecimalValue.java as follows:

 

GetDecimalValue.java

import java.util.*;
import java.text.*;

public class GetDecimalValue
{
  public static void main(String args[]) {
  // Decimal Value in the 12345.68 format
  // By rounding the last digit
  DecimalFormat dec = new DecimalFormat("###.##");
  System.out.println(dec.format(12345.6789))

  // Decimal Value in the 12345.679 format
  // By rounding the last digit
  dec = new DecimalFormat("###.###");
  System.out.println(dec.format(12345.6789))

  // Decimal Value in the 12345.7 format
  // By rounding the last digit
  dec = new DecimalFormat("###.#");
  System.out.println(dec.format(12345.6789));
  }
}

Output:

C:\javaexamples>javac GetDecimalValue.java

C:\javaexamples>java GetDecimalValue
12345.68
12345.679
12345.7

Download Source Code

 

Related Tags for Java get decimal value:
cstringclassormformformatgetdecimalpatternvalueifforiecalllongtocisheilitlscallingcanlipeiminrmcalasmtrcaclspecallsspsoatislllfstrwisvattssriringthstaludecimalformatlsoono


More Tutorials from this section

Ask Questions?    Discuss: Java get decimal value  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.