• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NumberFormat help!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try

 
Amir JavaGuy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use a real name as the forum asks you to, you might get more replies!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic