1

Im working on a cash register type application, i'm running into problems when separating dollars and cents. I can get dollars by its self but not cents. This is where I'm currently at.

change 126.7328 dollars 126 cents 7328

I want cents to just be 73 instead of 7328.

    String changeString = Double.toString(change); 
    String[] parts = changeString.split("\\.");
    String part2 = parts[1]; 
    double cents5 = Double.parseDouble(part2);
    int cents = (int)cents5

Any help would be much appreciated.

user3808597
  • 47
  • 1
  • 3
  • 12

1 Answers1

0

Format your data using a NumberFormat object:

NumberFormat myCurrencyFormat = NumberFormat.currencyInstance();
String moneyString = myCurrencyFormat.format(change);

Or if you don't want to see the money symbol, use a DecimalFormat:

DecimalFormat decimalFormat = new DecimalFormat("0.00");
String moneyString = decimalFormat.format(change);
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373