Jump to content
xisto Community
jimmy89

Help Rounding Maths Values

Recommended Posts

Hi,
I am making a program that calculates prices from somewhere like a supermarket and need to be able to calculate prices. I would like it to be able to round to 5 cents (as that is our currency in Australia). I have so far got it to round to the 5c, but it is always rounding down. so if the total came to $1.99, the rounded total would come out as $1.95.

This is the code I have so far

Dim total, amount As Decimal		amount = Me.amount.Text		total = Me.total.Text.Trim("$")		total = total + amount		Me.total.Text = "$" + total.ToString		Dim offset, value, temp, result As Decimal		offset = 0.05		value = Me.total.Text.Trim("$")		temp = value / offset		temp = System.Math.Truncate(temp)		result = temp * offset		Label1.Text = "$" + result.ToString
All I need is for it to round up or down depending on what the price is.
Thanks
-jimmy
Edited by Jimmy89 (see edit history)

Share this post


Link to post
Share on other sites

Well first you need to decide if you want to actually round it, or just drop it to the 0.05 below it. What I mean is, normally 1.99 would round to $2.00 not $1.95 if rounding to the nearest 5 cents :PPast that, personally my method would be to do something along the lines of taking the value, in cents, and modding by 5. If the remainder (value after modding) is 0, 1, or 2, return the value minus that remainder (dropping 1.92 for example to 1.90) or if it is 3 or 4, it could return the value plus 5 minus the remainder (bringing 1.93 to 1.95, for example).I hope that makes sense as I just got to work so don't have the time to put it into actual code form <_<

Share this post


Link to post
Share on other sites

Dim total, amount As Decimal		amount = Me.amount.Text		total = Me.total.Text.Trim("$")		total = total + amount		Me.total.Text = "$" + total.ToString		Dim offset, value, temp, result As Decimal		offset = 0.05		value = Me.total.Text.Trim("$")		temp = value / offset		temp = System.Math.Truncate(temp)		result = temp * offset		Label1.Text = "$" + result.ToString
Here is some sample code as what Jeigh had suggested. The only thing you need to take note of is that mod only works for integer, so you need to scale up the currency by 100, then scale down later
Dim total, amount As Decimal		amount = Me.amount.Text		total = Me.total.Text.Trim("$")		total = total + amount		Me.total.Text = "$" + total.ToString		Dim offset, value, temp, result As Decimal		offset = 0.05 * 100 'Scale Up		value = Me.total.Text.Trim("$") * 100 'Scale Up		temp = value Mod offset 'To Get the remainder		result = value - temp		If temp > 3 Then 'Pick a value to round up				result += 1		End If		result = result /100 ' Remember to scale back		Label1.Text = "$" + result.ToString
That should do it. I didn't code it in visual studio, if you see syntax error, you should know what to do. The concept is there.

Good Luck

Share this post


Link to post
Share on other sites

with the new code, when the value gets to say $1.94, it rounds it back to $1.91 and when it gets to $1.99, it rounds to $1.96. Both of those don't work, but everything else rounds perfectly

Share this post


Link to post
Share on other sites

Awesome, thanks for throwing that into code form faulty, mine woulda been pseudo-code anyways since I haven't used strict vb in awhile haha. Good stuff <_<Oh and its not like I mind, but just curious, is this for homework? hah

Share this post


Link to post
Share on other sites

well, last year as part of our programming course, we made a simple program (this was way back at the start) that calculated numbers in a textbox. We then, in class, adapted it to a kind of Point-of-sale program, which had its flaws. Recently, i was going over the old archives of work looking for something and found the project. So this was just trying to update it a bit!-jimmy

Edited by Jimmy89 (see edit history)

Share this post


Link to post
Share on other sites

Oh cool, yea it's not like I cared about helping someone on homework, it just had a real computer-science-assignmenty feel to the question so figured I'd ask <_< my curiosity always gets the best of me so I dont bother fighting it anymore.

Share this post


Link to post
Share on other sites

Well, In some ways you were right! It was originally from an assignment! And whoever said that curiosity killed the cat, i say the cat should have been more careful! <_<

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.