Division of Dollar value

Z

zzyzx

Hello All,

I wish to do manipulations such as
$10.00/$12.00 = answer

Any comments, code suggestions or offerings are appreciated.

Steve
 
T

Thomas Kellerer

Hello All,

I wish to do manipulations such as
$10.00/$12.00 = answer

Any comments, code suggestions or offerings are appreciated.

Steve

public class GetAnswer
{
public static void main(String args[])
{
int a = 10;
int b = 12;
int answer = a/b;
System.out.println("New amount = $" + answer);
}
}

Or did I not understand your question correctly?

Thomas
 
Z

zzyzx

Your reponse is appreciated however it may be difficult to implement
with the dollar value being a String which contains a "$" and a comma to
delineate thousands in its initial form in the database.

How does one efficiently convert the Strings to int or floats for division.

String price = "$1,000";
int salePrice = 0;
// to int i from String g try {
salePrice = Integer.parseInt(price.trim());
} catch (NumberFormatException e){}

fails

also;
// to int i from String g try {
salePrice = Float.parseFloat(price.trim());
} catch (NumberFormatException e){}

also fails due to dollar sign and comma contained in the string.

Is there an API library with the magic to convert the string format in
the database to a useful math ready state. Maybe something in the
locale or math classes


Thomas said:
Hello All,

I wish to do manipulations such as
$10.00/$12.00 = answer

Any comments, code suggestions or offerings are appreciated.

Steve


public class GetAnswer
{
public static void main(String args[])
{
int a = 10;
int b = 12;
int answer = a/b;
System.out.println("New amount = $" + answer);
}
}

Or did I not understand your question correctly?

Thomas
 
Z

zzyzx

Hello,

Am I understanding correctly that
public static long parseLongPennies( String numStr )

converts dollars to pennies while also removing locale sensitive
indicators such as commas and dollar signs...

Steve
 
R

Roedy Green

String price = "$1,000";

write a loop that looks at each char in turn and discards it if it is
$ or , and if not, append to a StringBuffer. Then convert that string.

Keep in mind Europeans reverse the meaning of , and .
 
R

Roedy Green

public static long parseLongPennies( String numStr )

converts dollars to pennies while also removing locale sensitive
indicators such as commas and dollar signs...

you'd have to look at the code or the JavaDoc. It would be pretty
easy to add that feature.
See http://mindprod.com/products.html#BUS in the Misc class.

I wrote a method called parseDirtyLong which just ignores any strange
chars. It is part of http://mindprod.com/products.html#BUS in the Misc
class.

I also wrote one called parseDirtyDollar which is part of
http://mindprod.com/products.html#AMERICANTAX

Look at those examples, and you should be able to create a variant to
do what you want.
 
S

Steve Jasper

My knee-jerk reaction would be to use a BigDecimal for these data
types, so you won't lose any precision (which can be a problem for
doubles/Doubles I'm told).

Take it with a grain of salt though, I'd love to hear from everyone
else....
 
M

Michael Borgwardt

Roedy said:
write a loop that looks at each char in turn and discards it if it is
$ or , and if not, append to a StringBuffer. Then convert that string.

Et voila, you've written an inferior replacement of DecimalFormat...
 
M

Michael Borgwardt

Steve said:
My knee-jerk reaction would be to use a BigDecimal for these data
types, so you won't lose any precision (which can be a problem for
doubles/Doubles I'm told).

No. You always lose precision. The imortant point is that BigDecimal
allows you to specify very exactly *how* that loss of precision (usually
known as "rounding") is to be performed. Real financial applications
must obey rules for that, often mandated by law. BigDecimal makes it
easy to implement those rules.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top