NumberFormat question

Double dbl =new Double(123344.9987D);

Locale locale =new Locale("en","US");

NumberFormat nf = NumberFormat.getInstance(locale);

nf.setMaximumFractionDigits(2);

nf.setMinimumFractionDigits(2);

String result = nf.format(dbl.doubleValue());

System.out.println("result [" + result +"]");

This code prints 123,345.00, but I would like to print 123,344,99. How?

Thanks!

[588 byte] By [DeepPurpleDeepa] at [2007-11-27 5:30:51]
# 1

> > Double dbl = new Double(123344.9987D);

> Locale locale = new Locale("en", "US");

> NumberFormat nf = NumberFormat.getInstance(locale);

> nf.setMaximumFractionDigits(2);

> nf.setMinimumFractionDigits(2);

> String result = nf.format(dbl.doubleValue());

> System.out.println("result [" + result + "]");

>

>

> This code prints 123,345.00, but I would like to

> print 123,344,99. How?

>

> Thanks!

So you want no rounding at all ?

Aknibbsa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 2
> So you want no rounding at all ?Yes, no rounding. Thanks!
DeepPurpleDeepa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 3
what about multiply by 100 truncate the number then divide by 100 and do your set value ?That is the best thing that comes to mind there are likely better options though.Why would you want to do that ? (Just curious what situation that would be useful )
Aknibbsa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 4
Put your number into a BigDecimal object. Then convert that into a new BigDecimal object that contains what you want by calling its setScale() method and specifying a rounding mode.
DrClapa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 5
It would be nice if NumberFormat let you adjust the rounding mode, not?
Hippolytea at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 6
> It would be nice if NumberFormat let you adjust the> rounding mode, not?check out the rounding mode options.
Aknibbsa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 7

> > It would be nice if NumberFormat let you adjust

> the

> > rounding mode, not?

>

> check out the rounding mode options.

Everyday is Saturday night, but I can't wait for Sunday morning, Sunday morning!

Yet another cool 1.6 improvement! I should start paying attention! Demo:import java.math.*;

import java.text.*;

public class RoundingExample {

public static void main(String[] args) {

NumberFormat f = NumberFormat.getNumberInstance();

System.out.format("default format:%s%n", f.format(Math.PI));

f.setMinimumFractionDigits(10);

f.setMaximumFractionDigits(10);

System.out.format("10 fraction digits: %s%n", f.format(Math.PI));

f.setMinimumFractionDigits(7);

f.setMaximumFractionDigits(7);

System.out.format("7 fraction digits: %s%n", f.format(Math.PI));

f.setRoundingMode(RoundingMode.FLOOR);

System.out.format("7 fraction + floor: %s%n", f.format(Math.PI));

}

}

Hippolytea at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 8

> what about multiply by 100 truncate the number then

> divide by 100 and do your set value ?

> That is the best thing that comes to mind there are

> likely better options though.

>

> Why would you want to do that ? (Just curious what

> situation that would be useful )

Yes, multiplying by 100 then divide would be an option. But I'm think rounding operation should not be related to a formatting class like NumberFormat. I guess this operation is more related to the number itself and not to a formatting operation.

I thought at least there was a method like setRounding(false), but I didn't find anything.

DeepPurpleDeepa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 9
> Why would you want to do that ? (Just curious what> situation that would be useful )Requirements in the project, :-((
DeepPurpleDeepa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 10
> Put your number into a BigDecimal object. Then> convert that into a new BigDecimal object that> contains what you want by calling its setScale()> method and specifying a rounding mode.I like this. Thanks!
DeepPurpleDeepa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 11
Are you sure you don't want:f.setRoundingMode(RoundingMode.FLOOR)
Hippolytea at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 12

> > > It would be nice if NumberFormat let you adjust

> > the

> > > rounding mode, not?

> >

> > check out the rounding mode options.

>

> Everyday is Saturday night, but I can't wait for

> Sunday morning, Sunday morning!

> Yet another cool 1.6 improvement! I should start

> paying attention! Demo:[code]import java.math.*;

> import java.text.*;

>

> public class RoundingExample {

>public static void main(String[] args) {

> NumberFormat f =

> NumberFormat.getNumberInstance();

> System.out.format("default format:%s%n",

> f.format(Math.PI));

>

> f.setMinimumFractionDigits(10);

> f.setMaximumFractionDigits(10);

> System.out.format("10 fraction digits: %s%n",

> f.format(Math.PI));

>

> f.setMinimumFractionDigits(7);

> f.setMaximumFractionDigits(7);

> System.out.format("7 fraction digits: %s%n",

> f.format(Math.PI));

>

> f.setRoundingMode(RoundingMode.FLOOR);

> System.out.format("7 fraction + floor: %s%n",

> f.format(Math.PI));

>}

> ode]

Where is this method, System.out.format ? Thanks!

DeepPurpleDeepa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 13

> Where is this method, System.out.format ? Thanks!

Are you using the Big Boy Version of Java: [url=http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html#format(java.lang.String,%20java.lang.Object...)]http://java.sun.com/javase/6/docs/api/java/io/PrintStream.html#format(java.lang.String,%20java.lang.Object...)[/url]?

Hippolytea at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 14
> Are you sure you don't want:> > f.setRoundingMode(RoundingMode.FLOOR)> Where is setRoundingMode() method? In the NumberFormat class? I didn't find that!Thanks.
DeepPurpleDeepa at 2007-7-12 14:55:30 > top of Java-index,Java Essentials,Java Programming...
# 15

Same Big Boy Version: [url=http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html#setRoundingMode(java.math.RoundingMode)]http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html#setRoundingMode(java.math.RoundingMode)[/url].

Joking aside, as you can see from the API, this method was added in the

current version of Java (1.6). You must be using one of them old versions.

Hippolytea at 2007-7-21 21:30:02 > top of Java-index,Java Essentials,Java Programming...
# 16

> Same Big Boy Version:

> [url=http://java.sun.com/javase/6/docs/api/java/text/N

> umberFormat.html#setRoundingMode(java.math.RoundingMod

> e)]http://java.sun.com/javase/6/docs/api/java/text/Num

> berFormat.html#setRoundingMode(java.math.RoundingMode)

> [/url].

>

> Joking aside, as you can see from the API, this

> method was added in the

> current version of Java (1.6). You must be using one

> of them old versions.

Yes... and I will cry now :-((

Thank you!

DeepPurpleDeepa at 2007-7-21 21:30:02 > top of Java-index,Java Essentials,Java Programming...
# 17

> Put your number into a BigDecimal object. Then

> convert that into a new BigDecimal object that

> contains what you want by calling its setScale()

> method and specifying a rounding mode.

Sorry, I don't know what mode is better in my case. I see that either BigDecimal.ROUND_DOWN or BigDecimal.ROUND_FLOOR does what I need. I read the documentation about them, but I still don't know. Which is better?

Thanks

DeepPurpleDeepa at 2007-7-21 21:30:02 > top of Java-index,Java Essentials,Java Programming...
# 18

I don't suppose I could offer a lo-fi hack?

public class CheapRoundingHack {

public static void main(String[] args) {

Double dbl = new Double(123344.9987D);

int rounder = (int) (dbl * 100.0);

double result = (double)rounder/100;

System.out.println("result [" + result + "]");

}

}

kevjavaa at 2007-7-21 21:30:02 > top of Java-index,Java Essentials,Java Programming...
# 19
Depends on what you mean by "better". Generally you want to choose the one that does what you want. Note that ROUND_DOWN and ROUND_FLOOR work the same for positive numbers but differently for negative numbers.
DrClapa at 2007-7-21 21:30:02 > top of Java-index,Java Essentials,Java Programming...