• 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

Precesion setting

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell me how to set precesion of a number(double). Say I have a number 9.123456 and I want to set it to 9.12. Is there any readymade method available or I have to write a new method for that.
Thanks.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the various methods provided by java.lang Number Many of them involve extracting a lower-precision value from a higher-precision content which means the operation will involve rounding.
Double, Long, Integer etc extends java.lang Number and hence you can use one of these methods.
Hope that helps!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Rakesh Sharma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajith
As suggested by you , I looked the API for the same but couln't get what I want. I will be thankful to you if you can give me specific site address where i can look for it.
Thanks
Rakesh Sharma
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajesh,
I think I misunderstood your question. I thought you just wanted to truncate the number. But when I read your question again, I realized you want to set the contents to a different number. Is that what you want to do?
ie., Double d = new Double(9.12345678)

and now, at some later point you want to set d to another number 9.12 ? Is that right??
If so, you can create another Double object with the desired value and simply assign it to d.
id., d = new Double( 30.789 )

Does that answer your question??
[This message has been edited by Ajith Kallambella (edited April 02, 2001).]
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rakesh,
If you want to format the double, then you can do like the following.
double variable = 9.123843;
NumberFormat nformat = NumberFormat.getInstance();
DecimalFormat dformat = (DecimalFormat)nformat;
dformat.applyPattern("#,##0.00;(#,##0.00)");
System.out.println(dformat.format(variable));
output : 9.12
-Shoba
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic