Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

How to set double fraction Precision

Wed Feb 13, 2008 9:53 pm

How to set double fraction Precision in Java?

i was doing something in my program and i found myself that i need to approximate a fraction to half upper, i googled and i didn't find a fine answer, but after some tries i figured it out by myself, its something easy all you have to do is

Code:

import java
.math.BigDecimal;

public class Main {

    public static void main(String[] args) {
        double  num = 0.07568;
       1-  BigDecimal b = new BigDecimal(num).setScale(2,BigDecimal.ROUND_HALF_UP);
       2-  num=b.doubleValue();
         System.out.println("Value is : "+ num);
        
    
}

}

 


in line 1- we created an object from BigDecimal that takes a double number and with the setScale function you can scale it to any approximation you want, and here we approximated it until 3 values after the dot, and we choose to round it to the upper half, you have the ability to round it to different ranges according to your needs .

in line 2- we converted it to double and output it.



Post a reply
  Related Posts  to : How to set double fraction Precision
 displaying double quotes in ASP     -  
 double value be cast to a byte     -  
 using double font families with body tag     -  
 Scale double number using big decimal     -  
 double buffering-applet animates graphics     -  
 Add double quotes to text dynamically, on page load     -  
 Program to solve equations using double linked list     -  
 Read double values type from file in java     -  
 click,double click,focus,blur and hover actions in JQuery     -  
 get time difference between to mouse-clicks-double clicks     -  

Topic Tags

Java Variables