Solved

Ridding Myself from Trailing Digits on Dollar Amts

Posted on 1998-08-14
4
233 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
Java SE 8u111  Lot of stuff broke 11 69
expectj telnet failing 5 36
SHA2 certs for IIS AND Java? 2 88
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

773 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