Solved

Ridding Myself from Trailing Digits on Dollar Amts

Posted on 1998-08-14
4
228 Views
Last Modified: 2010-05-18
I'm trying to get a dollar amount in the format of $xx.xx.  Using the double datatype, when I multiply two numbers, sometimes I get a format of $xx.00000000007.  I know its stupid, but how can I restrict the output to only two digits after the decimal point?
0
Comment
Question by:earlmoore
4 Comments
 

Author Comment

by:earlmoore
ID: 1232862
Edited text of question
0
 

Expert Comment

by:awilkins
ID: 1232863
One way to do this would be to ues a long instead of a double to represent your dollar amount.  This long would then represent the total number of cents.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1232864
Whenever I need to deal with dollars, I avoid float and double like the plague. When you multiply and divide, you can easily get results that are ever so slightly off. I have found it far better to stick with int or long. Multiply the dollar values, add 50 and divide by 100 for a rounded result. (Add 0 to truncate, and 99 to round high).
Output can be done with something like:

StringBuffer dv=new StringBuffer(Integer.toString(dlr));
dv.insert(dv.length()-2,'.');
String dlrstr=dv.toString();

This produces the right string, and could be captured in some utility method.


0
 
LVL 2

Accepted Solution

by:
threshold earned 100 total points
ID: 1232865
In Java, JVM stores the double/float into binary code(IEEE 754)
if you assign 1.05 into double a, there is no binary code that can represent 1.05 exactly.
The JVM just choose the nearest one, so you will got 1.0500000000004...

To calculate currency, you should :
1. use the BigDecimal to calculate/print currency explicitly.
2. use the double and DecimalFormat to print double.

String input_string=myTextField.getText(); // maybe you need to skip the '$' first.
double a=(new Double(input_string)).doubleValue();
DecimalFormat format=new DecimalFormat('###,###,###.##');
System.out.println ("Result : "+format.format(a+0.005)); // to display the neighborhood in the format

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 90
Java Jpanels and Jframe 8 28
Java SE 8u111  Lot of stuff broke 11 54
difference of if loops 23 41
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now