Solved

how to round the double value

Posted on 2003-10-23
7
430 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

705 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

16 Experts available now in Live!

Get 1:1 Help Now