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!
> > 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 ?
> So you want no rounding at all ?Yes, no rounding. Thanks!
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 )
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.
It would be nice if NumberFormat let you adjust the rounding mode, not?
> It would be nice if NumberFormat let you adjust the> rounding mode, not?check out the rounding mode options.
> > 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));
}
}
> 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.
> Why would you want to do that ? (Just curious what> situation that would be useful )Requirements in the project, :-((
> 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!
Are you sure you don't want:f.setRoundingMode(RoundingMode.FLOOR)
> > > 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!
> 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]?
> 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.
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.
> 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!
> 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
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 + "]");
}
}
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.