How to format a double number

A

Asit

Hi all,
I have a double(primitive) value eg. 4.66666666668, I want this number
to be formatted upto 2 decimal places. When i use NumberFormat or
Decimal Format to format this double value it gives a result 4.67,
instead i want that the number 4.66666666668 must be formatted as
4.66.

Can somebody help ?

Regrds
Asit
 
M

Matthew Zimmer

Asit said:
Hi all,
I have a double(primitive) value eg. 4.66666666668, I want this number
to be formatted upto 2 decimal places. When i use NumberFormat or
Decimal Format to format this double value it gives a result 4.67,
instead i want that the number 4.66666666668 must be formatted as
4.66.

Can somebody help ?

Regrds
Asit

Well, you could always use the way java works with ints to solve this.
If you multiply a double by 100 and cast to an int, 4.6666666 would
change to 466. Then, divide that result by 100.0 (you need the .0 so
you get cast to a double) and you'll end up with 4.66. That way you
don't even have to use the NumberFormat class and everybody's happy!

Hope that helps.
Matthew
 
F

Filip Larsen

Asit wrote
I have a double(primitive) value eg. 4.66666666668, I want this number
to be formatted upto 2 decimal places. When i use NumberFormat or
Decimal Format to format this double value it gives a result 4.67,
instead i want that the number 4.66666666668 must be formatted as
4.66.

Truncate the number yourself towards inifinity (using floor) or zero
(using int cast) to the decimal place you want, then write it out:

double xFloor = Math.floor(x*100)/100; // 1.66 and -1.67
double xTrunc = ((int)(x*100))/100.0; // 1.66 and -1.66

However, I am very curious as to the reason why you want to truncate and
not round the number when printed?


Regards,
 
T

Tony Dahlman

Filip Larsen wrote:
However, I am very curious as to the reason why you want to truncate and
not round the number when printed?

Regards,

In other words, why does OP wish to make this mistake?
 
M

Michael Borgwardt

Asit said:
Hi all,
I have a double(primitive) value eg. 4.66666666668, I want this number
to be formatted upto 2 decimal places. When i use NumberFormat or
Decimal Format to format this double value it gives a result 4.67,
instead i want that the number 4.66666666668 must be formatted as
4.66.

Can somebody help ?

You could use java.math.BigDecimal, which allows you to specify
rounding modes for all operations, including setScale(), which
is what you're doing.
 
A

Alex Hunsley

Matthew said:
Well, you could always use the way java works with ints to solve this.
If you multiply a double by 100 and cast to an int, 4.6666666 would
change to 466. Then, divide that result by 100.0 (you need the .0 so
you get cast to a double) and you'll end up with 4.66. That way you
don't even have to use the NumberFormat class and everybody's happy!

Hope that helps.
Matthew

That sounds a bit computationally expensive, wouldn't it be a bit easier
to consider the number as a string and then find the postiion of the "."
and truncate the string 2 chars after that?

alex
 
M

Matthew Zimmer

Alex said:
That sounds a bit computationally expensive, wouldn't it be a bit easier
to consider the number as a string and then find the postiion of the "."
and truncate the string 2 chars after that?

alex

Well, floating point arithmetic is fairly cheap in the grand scheme of
things, so I don't think it'd be prohibitively expensive. In addition,
there would be none of the method overhead that you'd get with Strings.
However, you could do it the way you suggest, but you'd have to be a
lot more careful in terms of what you're doing. For example, you'd have
to check to make sure that there are enough chars after the decimal so
you don't get a size exception (ie 4.5 will crash if you try to get two
chars after the decimal). You'd also need to make sure that there WAS a
decimal point in the String.

Here's what the two different ways of doing this would be (including all
the checks you'd have to make to ensure that there would be no
possibility of an exception). Given double x:

x = ((int)(x*100))/100.0;

OR

String n = new String(x);
int idx = n.indexOf('.');
if (idx != -1 && idx < n.length()-2)
{
x = Double.parseDouble(n.substring(0, idx+3));
}


To me it seems like the aritmetic would be cleaner and faster, but I'd
have to run a lot of emperical tests to be sure and it's just not worth
it to me! :)
 
Joined
Mar 14, 2011
Messages
1
Reaction score
0
Refer the code

Hi,

You can truncate any number using the decimal format. But for that you need to use RoundingMode as Floor. Please find below the code, execute this using different values of variable double i.

import java.text.DecimalFormat;
import java.math.RoundingMode;
public class DecFormat {

public static void main(String[] args) {

//final DecimalFormat decFormat = new DecimalFormat("0.00");
//System.out.println("decFormat =" +decFormat.format(new Float(1234.56898)));
double i = 69.877;
DecimalFormat format = new DecimalFormat("#.##");
format.setRoundingMode(RoundingMode.FLOOR);
String s = format.format(i);
i = Double.parseDouble(s);
System.out.println(i); //should be 69.8
}


}

This will work fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top