Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormat help!

 
Amir JavaGuy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How do I create a method that formats a number based on it's string value ? Lets say I have the number 1027

I want to call my format routine to return ( lets say my locale is set to English US ) 1,027

If I passed 1027.000 to my routine, I want it to return 1,027.000

I have written the following routine to achieve this goal, but for some reason, any number I pass in with trailing zero's get truncated. If I pass in 1027.123, it works fine. If I pass in 1027.000, it returns 1,027 ! Am I missing something basic ?

Here's the snippet :



protected static String embedProperDelim (String s,
int coding) {

java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
Number myNumber = null;

try {
myNumber = nf.parse (s);
} catch (java.text.ParseException e) {
e.printStackTrace ();
}

String formatted = nf.format(myNumber);

return formatted;


}

Any help is much appreciated.
 
Ed Ewing
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

 
Amir JavaGuy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ed,

Thank you for the quick response. This works when I pass in 1027.000

The result is 1027.000 as I wanted

BUT, what about when I pass in 1027 ? Here, I'd like it to be smart enough to pring out 1027
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have to count traling "decimal-zeros" of your original number and append them after formatting.

Or count the decimal digits and format your number with this minimum value.
[ October 14, 2005: Message edited by: Seb Mathe ]
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a real name as the forum asks you to, you might get more replies!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic