Talk2Tom11 Posted February 8, 2006 Posted February 8, 2006 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 Quote
kejpa Posted February 8, 2006 Posted February 8, 2006 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 Quote
Talk2Tom11 Posted February 8, 2006 Author Posted February 8, 2006 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. Quote
kejpa Posted February 8, 2006 Posted February 8, 2006 25/25 = 1 , remainder is 0 35/25 = 1 remainder is 10 The mod operator gives you the remainder not the number of quarters. /Kejpa Quote
Talk2Tom11 Posted February 8, 2006 Author Posted February 8, 2006 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. Quote
Talk2Tom11 Posted February 8, 2006 Author Posted February 8, 2006 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 Quote
Cags Posted February 8, 2006 Posted February 8, 2006 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 Quote Anybody looking for a graduate programmer (Midlands, England)?
Talk2Tom11 Posted February 8, 2006 Author Posted February 8, 2006 I am not near my computer with vb... so i have not tried it... but what you are saying makes sense. Thank you very much for your help. I will reply as to how it turns out. Quote
Talk2Tom11 Posted February 8, 2006 Author Posted February 8, 2006 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 Quote
kejpa Posted February 8, 2006 Posted February 8, 2006 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.