Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need a function that converts a double into a whole int, rounding the decimal and ret

 
Zubi Pen
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey peeps,

I am new to java and I wanted to know how to make a little function that has a double value and returns a int value in form of a string.
For example,



So after running the function it returns the value: 12,346

Hence this does two things, places a comma afer the three digits and rounds the document in a whole number so 12345.56 becomes 12346

Can anyone help me how to do this.

Thanks,
Zub
 
Ragav Baskaran
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try java.text.DecimalFormat class for formating.
* Use format method

Use java.lang.Math for rounding off numbers.

* Math.round(<double>

Hope this helps.

Thanks
Ragavendran

[ September 02, 2008: Message edited by: Raga Vendran ]
[ September 02, 2008: Message edited by: Raga Vendran ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic