[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# how to round the double value

Posted on 2003-10-23
Medium Priority
453 Views
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
0
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

LVL 15

Expert Comment

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

LVL 4

Expert Comment

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

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

LVL 15

Expert Comment

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

ID: 9608032
You're right ;-)
0

LVL 2

Accepted Solution

f_98 earned 150 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

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

## Featured Post

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
###### Suggested Courses
Course of the Month13 days, 19 hours left to enroll