Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Help Rounding Maths Values
Jimmy89
post Jul 5 2007, 10:45 AM
Post #1


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 708
Joined: 30-June 06
From: Australia
Member No.: 14,219
myCENTs:76.93



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
CODE
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

This post has been edited by Jimmy89: Jul 5 2007, 10:46 AM
Go to the top of the page
 
+Quote Post
Jeigh
post Jul 5 2007, 11:04 AM
Post #2


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,371
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281
myCENTs:65.99



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 wink.gif

Past 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 wink.gif
Go to the top of the page
 
+Quote Post
faulty.lee
post Jul 5 2007, 11:43 AM
Post #3


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:NEGATIVE[-20.12]



QUOTE(Jimmy89 @ Jul 5 2007, 06:45 PM) *
CODE
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
CODE
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
Go to the top of the page
 
+Quote Post
Jimmy89
post Jul 5 2007, 12:01 PM
Post #4


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 708
Joined: 30-June 06
From: Australia
Member No.: 14,219
myCENTs:76.93



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
Go to the top of the page
 
+Quote Post
faulty.lee
post Jul 5 2007, 12:09 PM
Post #5


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:NEGATIVE[-20.12]



Sorry, i made a mistake. it should be
result += 5
Go to the top of the page
 
+Quote Post
Jimmy89
post Jul 5 2007, 12:23 PM
Post #6


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 708
Joined: 30-June 06
From: Australia
Member No.: 14,219
myCENTs:76.93



Thanks heaps! Works perfectly!
-jimmy
Go to the top of the page
 
+Quote Post
Jeigh
post Jul 5 2007, 01:14 PM
Post #7


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,371
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281
myCENTs:65.99



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 biggrin.gif

Oh and its not like I mind, but just curious, is this for homework? hah
Go to the top of the page
 
+Quote Post
Jimmy89
post Jul 6 2007, 06:47 AM
Post #8


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 708
Joined: 30-June 06
From: Australia
Member No.: 14,219
myCENTs:76.93



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

This post has been edited by Jimmy89: Jul 6 2007, 06:47 AM
Go to the top of the page
 
+Quote Post
Jeigh
post Jul 6 2007, 02:02 PM
Post #9


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,371
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281
myCENTs:65.99



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 biggrin.gif my curiosity always gets the best of me so I dont bother fighting it anymore.
Go to the top of the page
 
+Quote Post
Jimmy89
post Jul 8 2007, 02:01 AM
Post #10


Living at the Datacenter
Group Icon

Group: [HOSTED]
Posts: 708
Joined: 30-June 06
From: Australia
Member No.: 14,219
myCENTs:76.93



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! tongue.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Vedic Maths: Any Takers ?(7)
  2. Something To Ponder Upon... ;) 0.9 = 1(6)
  3. Some Fun With The Calculator(7)
  4. A Maths Problem Hmm.(9)
  5. The Question Of Ethics(5)
  6. Ajax+php+sql=simply Superb!(with Visitor Tracking) :: Section 2 (retrive Values From Database And Dynamic Update!)(2)
  7. Need Help With Rounding Integers(5)
  8. [help] Gridview In Asp Not Updating Values(0)
  9. Converting Alphanumeric Strings Into Numerical Values(4)
  10. Updating Values In A Text Box(11)
  11. Extracting Mysql Maths Using Php(2)


 



- Lo-Fi Version Time is now: 23rd November 2008 - 06:47 PM