• 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

need a double returned with 2 decimal points

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to return a double value with two decimal points ( even if the last decimal value = 0) I have used numberFormat but returns a string and when I convert the string back to double i lose the last decimal vale if equal to 0.

eg from numberFormat i get a string with value 23.30, and when i convert it to double, it gives 23.3

any suggestions will be greatly appreciated.

Thanks

Anil
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A double doesn't have any number of decimal points. You can try rounding it to 2dp precision, which is as near as you will get. There is the Math.round() method, but you will have to multiply and divide to use that.
You can use several methods, one of which you have already found, to display it with 2dp.
 
AnilPrakash Raju
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell,

I do round it off to 2 decimal points, but if the last digit is 0, i get only decimal point.

Anil
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by AnilPrakash Raju:
Thanks Campbell,

I do round it off to 2 decimal points, but if the last digit is 0, i get only decimal point.

Anil




No... You don't get only 1 decimal point. You don't get any number of decimal points. There is *no* formatting to a double. Period.

The only time that you "get" a number of decimal points, is when you print it out (output of the program). In this regard, what is wrong with using NumberFormat? So, it's a string? You are only going to print it out anyway. Use NumberFormat to help with outputing the result -- and leave your double alone.

Henry
 
AnilPrakash Raju
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry
reply
    Bookmark Topic Watch Topic
  • New Topic