Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello I want to convert a money amount into dollars and coins.

 

for example if i start with $20.35.

 

it should tell me that i have 20 dollars, 1 quarter, and 1 dime.

 

I have been working using the mod keyword to hold the remainder but it just isn't coming out right... here is my code.

 

intamount = val(txtinput.text)
intamount = dblamount * 100
dollars = intamount \ 100
quarters = intamount mod 100
intamount = intamount \ 100
quarters = intamount \ 25

 

when i input $20.25... it gives me... 20 dollars, and 0 quarters

 

???

 

Does anyone know what i am doing wrong in my code.

 

If you can help then please post

Posted

You shouldn't use 'mod' to find out the number of quarters, mod gives you the remainder and that's 0 if you divide 25 by 25. You should use the whole-part as the number of quarters and keep the remainders for the daims.

 

dblamount=val(txtinput.text)
dollars=math.floor(dblamount)
remainder=dblamount-dollars
quarters=math.floor(remainder/0.25)
remainder=remainder-quarters*0.25
daims=math.floor(remainder/0.10)
remainder=remainder-daims*0.10

 

HTH

/Kejpa

Posted
You shouldn't use 'mod' to find out the number of quarters, mod gives you the remainder and that's 0 if you divide 25 by 25. You should use the whole-part as the number of quarters and keep the remainders for the daims.

 

/Kejpa

 

 

25 / 25 is 1. So i don't know why it would give me 0.

Posted
but i am only using mod for the dollars and then using that mod...which equals 25 and then divide that by 25 and displaying that. I am not using mod again on the remainder of 25 / 25.
Posted

I was actually thinking more on the lines of this code... but it still doesn't work

 

        dblamount = Val(TextBox1.Text)

       intamount = dblamount * 100
       d = intamount / 100
       intamount = intamount Mod 100
       intamount = intamount / 100
       q = intamount / 25

       TextBox2.Text = d
       TextBox3.Text = q

Posted
        dblamount = Val(TextBox1.Text)

       intamount = dblamount * 100
       d = intamount / 100
       intamount = intamount Mod 100
       intamount = intamount / 100
       q = intamount / 25

       TextBox2.Text = d
       TextBox3.Text = q

 

Not sure why you were dividing by 100 again, I can't see that its needed.

dblamount = 20.25
'convert to cent (i think, don't know alot about american currency)
intamount = dblamount * 100
' find out how many dollars there are
dollars = intamount / 100 'Math.Floor() to find exact value?
' find remaining amount of cent
intamount = intamount mod 100
' find number of quarters
quarters = intamount / 25

Anybody looking for a graduate programmer (Midlands, England)?
Posted

it works but there is one problem... if i go over .50 then the dollars go up by one. for example... $10.51 is displayed as

 

11 dollars

2 quarters

1 penny

 

which it should be 10 dollars

Posted

converting from double to integer automatically rounds the value according to the regional settings rules. Use Math.floor instead which gives you the right answer.

 

           Dim dblamount As Double = Val("10.51")
           Dim dollars As Integer = Math.Floor(dblamount)
           Dim remainder As Double = dblamount - dollars
           Dim quarters As Integer = Math.Floor(remainder / 0.25)
           remainder = remainder - quarters * 0.25
           Dim daims As Integer = Math.Floor(remainder / 0.1)
           remainder = CInt((remainder - daims * 0.1) / 0.01)
           Console.WriteLine("{0}$ {1}q {2}d {3}p", dollars, quarters, daims, remainder)

Works swell both with 10.25,10.51 and 10.35

 

/Kejpa

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...