Solved

how to round the double value

Posted on 2003-10-23
7
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 110
Is there a simpler dropbox system? 10 53
Find out all repos that a user is most active on Github 1 56
Java ArrayList and if statement 2 55
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

737 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