Solved

how to round the double value

Posted on 2003-10-23
7
432 Views
Last Modified: 2010-03-31
if the number is 1.4567 then i need out is 1.46 so what to.
i am using java.lang.Math.round(1.4567) getting output is 1 but i want 1.46
please help me
0
Comment
Question by:schavalam
7 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9606365
Multiply it by 100, round it then divide it by 100.
0
 
LVL 4

Expert Comment

by:tapasvi
ID: 9607169
Hi schavalam:

use java.math.BigDecimal class. It is meant for this purpose only (currency fomatting, which as you know goes to 2 decimal points of precision like $23.75)

Taps


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9607783
>>getting output is 1 but i want 1.46

You must be assigning it to an integral variable such as int or long. Assign to a double or float
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:jimmack
ID: 9607981
CEHJ:  Math.round() returns a long or an int, so can't be used to round a double.

Not like you to miss that :-(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9608032
You're right ;-)
0
 
LVL 2

Accepted Solution

by:
f_98 earned 50 total points
ID: 9619005
if you just want to print it out,, try using a formatter(it automaticall rounds the value).

import java.text.DecimalFormat;

double d = 1.4567;
DecimalFormat df = new DecimalFormat(".00"); // set it to 2 decimal places
String output = df.format(d);

0
 
LVL 1

Expert Comment

by:bapi
ID: 9633272
Use the RoundUp/RoundDown option on BigDecimal.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 46
Image decoding from Camera 3 72
maven project error 5 48
servlet filter example 37 44
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now