1

I know there are ways to get the number of digits after a decimal point, for instance the substring method, but how would I go about doing this for the number of digits before a decimal place?

I need to use this to convert US change (double) into Euro change(double). The way I would like to do this is by taking the number before a decimal (such as $1.) and times it by its euro equivalent (.7498) and take the number after a decimal (.16) and times that by its .01 euro coin value (.0075), add both values together to get the euro equivalent of $1.16 (.8698).

2
  • What? Why don't you just multiply it as a floating point number (i.e. a double)? 1.16 * 0.7468 will give you the correct answer...
    – Björn
    Mar 27, 2012 at 14:13
  • 1
    I'm not convinced downvoting is meant to be used for questions where the OP has just got the wrong idea of how to do something - it will serve as a useful signpost for future peope if the correct method is highlighted - upvoting for that reason. Mar 27, 2012 at 14:20

4 Answers 4

7

To get the number before decimal point,do this:

String str = new Double(your_double_number).toString().subString(0,str.indexOf('.'));
double v = Double.valueOf(str);

If you are using '$' sign then take 1 in place of 0.

Hope it will help you.

0
2

convert US change (double) into Euro change(double)

Please don't do that. Never, never, ever use double or float to represent money, because those datatypes cannot represent most decimal fractions, so you get rounding errors before you even start to do any calculations.

Instead, use BigDecimal.

1

First of all - just multiplying the Dollar value by the exchange rate will get you the euro value so there's no need to do that as far as i can see - you will just introduce rounding errors.

But if you did need to - just use substring

String dollarVal = "$1.16"
String justFullDollar = dollarVal.substring(1, dollarVal.indexOf("."));
String justCents = dollarVal.substring(dollarVal.indexOf(".")+1);

The Correct way would be to store all you money as integers or arbitrary precision objects that way you get no floating point errors too.

Convert to cents, multiply and convert back again. e.g.

String dollarVal = "$1.16"
BigDecimal dollars = new BigDecimal(dollarVal.substring(1)); //1.16
BigDecimal cents = dollars.multiply(new BigDecimal(100)); //116
BigDecimal eurocents = cents.multiply(new BigDecimal(exchangeRate)); //86.9768
BigDecimal euros = eurocents.divide(new BigDecimal(100)); //0.869768
DecimalFormat formatter = new DecimalFormat("###.00");
String euroVal = "€" + formatter.format(euros);
10
  • THanks for that - was being an idiot for no good reason - fixed my example avoiding floating point and rounding errors Mar 27, 2012 at 14:37
  • Okay thankyou Matt. I'll study this and reply back with another other concerns I have.
    – Wormhole99
    Mar 27, 2012 at 15:13
  • ^^ When I run my program, using your method, instead of printing ".869768" like yours does, it prints. ".00869768"(...etc.)
    – Wormhole99
    Mar 28, 2012 at 23:31
  • What exchange rate are you using to multiply by? Mar 29, 2012 at 5:50
  • I was using .0075. I've figured out that I have to use .7528 ($1.00) to get the correct answer.
    – Wormhole99
    Mar 29, 2012 at 11:47
0

You can use

String s[] = new Double(your number).toString().split(".");

The s[0] is the number before decimal point. and s[1] is the number after decimal point.

Both are in String, so you need to parse them into double using

double num1 = Double.parseDouble(s[0]);
double num2 = Double.parseDouble(s[1]);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.