Solved

how to round the double value

Posted on 2003-10-23
7
434 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 37
Oracle SQL syntax check  without executing 6 50
reverse digits of a number using for loop 5 40
servlet example 17 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

815 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

12 Experts available now in Live!

Get 1:1 Help Now