• Post Reply Bookmark Topic Watch Topic
  • New Topic

Leading zeros  RSS feed

 
Hernan Tavella
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i have this issue with the leading zero, i tryed several ways with the same wrong result, what i am trying to do is to keep a leading zero when the amount is for example 0,4 because when i run the program it prints me ,4 can you help me please.
This is the code that i have:

[code=
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
// TODO Auto-generated method stub
arg2=arg2.replace(',', '.');

return new java.lang.Double(arg2);
}

java]public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
double monto = (Double)arg2;

NumberFormat formatter = new DecimalFormat("###,###,###.00");

return nf.format(monto);
}[/code]

But when the amont is for example 0,4 it prints ,4 is there any way to print the result with the leading zero?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hernan Tavella wrote:But when the amont is for example 0,4 it prints ,4 is there any way to print the result with the leading zero?

Are you sure it prints ,4 ? Or does it print ,40 ?
 
Hernan Tavella
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ,40 prints but i should be 0,40
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hernan Tavella wrote:Yes ,40 prints but i should be 0,40

So it prints trailing zeroes after the decimal point but not leading zeroes before it. What's the difference between how you are formatting numbers before the decimal point and after it ?
 
Hernan Tavella
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont understand you, if a print this number it prints ok 1.518.435,00 but when the value is under 0,4 for example it don't print correctly it print ,4 and the leading zero don't. Thank you
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the javadoc for the DecimalFormat class and see what the difference between # and 0 is.
 
Hernan Tavella
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tryed looking in that page but i didn't found the answer, any other suggestion? Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hernan Tavella wrote:I tryed looking in that page ...

Please explain in your own words what you see as the difference between 0 and #.
 
Hernan Tavella
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i use this pattern (NumberFormat formatter = new DecimalFormat("###,###,###.00");) to format the number that i input, the result is not what i expect when the value is for example 0,5 the result that it print it is ,5 and the zero disappear, whitn other value work find but when it is when a zero at the left y dont work well. Thank you
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hernan Tavella wrote:When i use this pattern...

so maybe you need to use a different pattern. The DecimalFormat API page explains what you could/should try.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!