LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-13-2004, 07:01 AM   #1
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 17

Rep: Reputation: 0
Java Precission rounding.


I'm writing a program that involves multiplying two numbers of double precision, but I need to display the numbers rounded to two decimal places without losing precion. How do I do this???

Thakns in advance...
 
Old 05-13-2004, 07:31 AM   #2
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 17

Original Poster
Rep: Reputation: 0
Meh, so far I have managed to reduce the double to only two numbers but doing the following...


temp = (int) (cost * 100);
cost = (double) temp / 100;

(cost is the number I want rounding)

However this just reduces the number form say 234.567 to 234.56 and it doesn't round it at all. How would I go about actually rounding it to 234.57.

thanks...
 
Old 05-13-2004, 08:35 AM   #3
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Something like this will do it....

Code:
import java.math.BigDecimal;

...blah...blah...

double myDouble=234.567544;
           
BigDecimal bd=new BigDecimal(myDouble).setScale(2,BigDecimal.ROUND_HALF_UP);
         
myDouble=bd.doubleValue();
take a look at the BigDecimal class to tailor it (although I'll bet there's more than one way )
 
Old 05-13-2004, 09:03 AM   #4
Tru_Messiah
LQ Newbie
 
Registered: May 2004
Posts: 17

Original Poster
Rep: Reputation: 0
Thank you, that seems to work....
 
Old 05-14-2004, 11:23 PM   #5
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 31
I noticed that your example is dealing with dollar amounts. If you want a nice, readable currency representation that is easy to format for different countries, you can use the NumberFormat class.

Code:
import java.text.NumberFormat;
import java.util.Locale;

public class NumberFormatExample {

    public static void main( String[] args ) {

        double cost = 123.456;
        NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
        System.out.println( currency.format( cost ) );
    }

}
There are many other ways of formatting the output using the NumberFormat class. Check it out in the API.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 02:13 AM
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 03:04 AM
HEELLP ~~ Converting an integer minute into time, Rounding off a double ~~ HELLP Mistro116@yahoo.com Programming 5 10-04-2005 11:51 AM
C++ Rounding and Truncation Opeth Programming 4 09-17-2005 07:16 PM
Web start java not working (java works fine) powadha Debian 5 06-05-2004 12:57 PM


All times are GMT -5. The time now is 04:43 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration