Total members 11895 |It is currently Sun Dec 22, 2024 3:32 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.



_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .


Author:
Expert
User avatar Posts: 838
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : How to set double fraction Precision
 double value be cast to a byte     -  
 displaying double quotes in ASP     -  
 Scale double number using big decimal     -  
 using double font families with body tag     -  
 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
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com