Seperating numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dargen
    New Member
    • Aug 2008
    • 2

    Seperating numbers

    The assinment is i gotta prompt user to enter the number of five, ten twenty, fifty cent coins and one and two dollar coins they have then add em together.
    The hard part is i need to store the number of cents as a integer and the number of dollars as an integer (not just one double)

    So yeah
    How can i seperate the number
    123456 into the two numbers
    1234 and 56

    To display $1234.56
    or any other way you could think of doing it

    Thanks for any help
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Make a class with an int cent variable and an int dollar variable.

    As for your specific question, if you are inputted 123456 and you want to get 1234 and 56 extracted from it you should do the following:

    First realize that 123456 = 6 *10^0 + 5*10^1 + 4*10^2 + 3*10^3 + 2*10^4 + 1*10^5.

    AND realize that when you divide an integer will truncate the remainder.

    With this you can do the following:

    store 123456 and make a second variable 123456

    divide by 100, you'll get 1234 (cause it's an int it'll throw the rest away). Now you have the first part. Then multiply by 100 again and you'll get 123400, subtract the original from that, 123456-123400 = 56, you have your second part, the cents.

    Good luck,
    -blazed

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by blazedaces
      With this you can do the following:

      store 123456 and make a second variable 123456

      divide by 100, you'll get 1234 (cause it's an int it'll throw the rest away). Now you have the first part. Then multiply by 100 again and you'll get 123400, subtract the original from that, 123456-123400 = 56, you have your second part, the cents.

      Good luck,
      -blazed
      Much easier:
      [code=java]int total_cash = 123456;
      int dollars = total_cash / 100;
      int cents = total_cash % 100;[/code]The % symbol is the so called modulo operator, which gives you the remainder of a division.
      So, as 123456 / 100 = 1234 + 56 / 100 it will give you 56.

      Greetings,
      Nepomuk

      Comment

      • Dargen
        New Member
        • Aug 2008
        • 2

        #4
        Tankyou so much guys!
        Great help :P

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          Originally posted by nepomuk
          Much easier:
          [code=java]int total_cash = 123456;
          int dollars = total_cash / 100;
          int cents = total_cash % 100;[/code]The % symbol is the so called modulo operator, which gives you the remainder of a division.
          So, as 123456 / 100 = 1234 + 56 / 100 it will give you 56.

          Greetings,
          Nepomuk
          Yes, much easier...

          -blazed

          Comment

          Working...