views:

161

answers:

8

Hi guys, I'm returning this, it is similar to how you percieve dollars, $"32.95" etc. I calculate it in cents which is an int, but the problem is the second half cuts off the 10s of cents part if the number is less than that. For example if I have "32.08" it returns as "32.8". Any ideas ? i know i need an if but i cant think how to write it.

public String toString()
{
    return (cents / 100)+ "." + (cents % 100);
}
A: 

Use the BigDecimal class, that's what it's for.

Francis Upton
+5  A: 

You can use http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also supports different kinds of numbers, including integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123). All of these can be localized.

String pattern = "$###,###.###";
double value = 12345.67;
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
System.out.println(value + " " + pattern + " " + output);
// => 12345.67 $###,###.### $12,345.67
The MYYN
that's a double!
irreputable
A: 

You could always check if cents % 100 is less than 10, and if it is, add another 0. There's probably a more elegant solution though.

Jan Gorzny
A: 

So, 32.08 % 100 is 8 not 08. You could of course add in a "0" for values less than 10.

However, you might want to think about using java.text, in particular NumberFormat, DecimalFormat and MessageFormat. java.util.Formatter and "printf" might be more appropriate if you are attempting to write machine readable text.

Tom Hawtin - tackline
A: 
NumberFormat nf=NumberFormat.getInstance(); // Get Instance of NumberFormat
nf.setMinimumIntegerDigits(5);  // The minimum Digits required is 5

return (cents / 100)+ "." + nf.format(cents % 100);

That should do it, been a while since I did java.

GrayWizardx
+2  A: 

The quick hack:

return String.format("%d.%02d", cents/100, cents%100);
Anon.
@Anon, you said to don't use float numbers in my deleted answer; what do you meant, as I was using double precision?
Rubens Farias
`double` is a floating-point number type.
Anon.
so bad to be used as money? So `The MYYN` answer is also a bad answer because its example uses double?
Rubens Farias
+1. I like this better than the answers that use a double or BigDecimal.
Jorn
The format should be "%d.%02d".
ZZ Coder
Thanks ZZ - `"%d.%2d" will pad with spaces, rather than zeroes. I'll fix it up.
Anon.
@Ruben: Yes, `The MYYN`'s answer is also bad because it uses a `double`. Floating-point types are not suited for money calculations because they have a limited precision and can't store all decimal numbers with infinite precision.
Jesper
A: 
(""+(100+cents%100)).substring(1) // LOL

(cents / 100)+ "." + (cents /10 %10) + (cents % 10); // <<== OK
irreputable
A: 

Where's the abstraction? Java's an object oriented language. Wouldn't it be a better idea to encapsulate all that implementation detail in a Money class and hide it from clients?

duffymo