1

Thanks for taking the time to answer my question.

I have a double value (let's say 22.35). I need to parse it into a String and get 2235. The following code is not working properly.

double totalPrice = 22.35;
DecimalFormat df = new DecimalFormat("#.##");
String[] temp = df.format(totalPrice).split(".");
String amount = temp[0] + temp[1];

I keep getting an exception ArrayIndexOutOfBounds. What is another way to do this? Thanks in advance!

4
  • 5
    int totalCents = (int)(22.35d*100); Commented May 21, 2012 at 2:04
  • @AndrewThompson - Looks like an answer to me, with perhaps a sentence or two for explanation. +1 Commented May 21, 2012 at 2:04
  • Why don't you just replace dot character by an empty one? Commented May 21, 2012 at 2:05
  • @jmort253 Great comment! :) Let's hope that it 'sounds better' (as in - does not get deleted) the way you said it. Commented May 21, 2012 at 2:15

4 Answers 4

4

If your values don't exceed, after multiplication by 100, MAX_INT, multiply them:

double totalPrice = 22.35;
int iPrice = (int) (totalPrice * 100);
String sPrice = "" + iPrice;
Sign up to request clarification or add additional context in comments.

3 Comments

@AndrewThompson: Sorry, forgot to cast.
@userunknown - Congrats on the 10k!
@jmort253: Thanks. The last 2000 happen by themselves.
0

What about:

double totalPrice = 22.35;
DecimalFormat df = new DecimalFormat("#.##");
String price = df.format(totalPrice).replace(".", "");

Comments

0

Maybe you can do it like this: just change the regex "." to " \."!

String[] temp = df.format(totalPrice).split("\\.");

Comments

0

This will work no matter how high a number you try to apply it to:

double num = 22.35;
String concatenate = "" + num;
String parsedNum = concatenate.replace(".", "");

Hope this helps you.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.