• 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

fixed length decimal numbers

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I get fixed length decimal number in java float or double?

say, i've 22 and want to devide it with 7.

i want to get 3.14 not 3.14285.....

help please.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java.text.NumberFormat class does this; check out in particular the setMaximumFractionDigits and setMinimumFractionDigits methods.
Of course, then you'll have a string, but you can convert it back easily enough.
[ September 29, 2005: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Multiply the result times 100.
2. round()
3. Divide by 100
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> I want to get 3.14 not 3.14285

In fact you really don't know what the value is until you observe it. So it makes sense to display it in the form you want while preserving the maximum precision the rest of the time.

double value = 3.143423432432;
System.out.printf("%.2f", value); // Prints: 3.14
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Rick points out, you should make a distinction between how the number is stored and how it is displayed. When doing calculations, you should store the value as precisely as possible. However, when you display it, you can use either NumberFormat or, if you can use Java 5.0 features, PrintStream.printf() for the exact formatting you want.

Layne
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another alternate to round a double value, make a java.text.DecimalFormat like:
static DecimalFormat df=new DecimalFormat("0.00");
use
System.out.println(df.format(aDoubleValue));
and it does the rounding.

--------------
Naveen Vooka
www.devsquare.com
DevSquare - Online Application Development
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic