Discuss Scratch

jacob_1986
New to Scratch
9 posts

rounding to nearest number?

I have written a program to give excess amount of money when a person pays but when the computer gives the amount the answer is not rounded? Moreover, when I enter 5.99 the computer thus gives the answer 1.240000000002, but if I enter a whole amount 5.00 it gives the answer -0.25?
deck26
Scratcher
1000+ posts

rounding to nearest number?

You need to share if you're getting such strange results because there has to be something wrong in your coding.

(round (5.99))    // should give 6
jacob_1986
New to Scratch
9 posts

rounding to nearest number?

My code is below, I have to enter a amount of money and give the excess amount correctly. Example: if I enter 5.00 I should receive the answer 0.25.

when green flag clicked
ask [ Enter Name? ] and wait
set [ name ] to (answer)
ask [ enter amount ] and wait
set [ money ] to (answer)
if <(money) > [4.75]> then
set [ excess ] to ((4.75) - (money))
say (join (name) (join [paid too much by ] (excess))) for (2) secs

end
jacob_1986
New to Scratch
9 posts

rounding to nearest number?

My code is below, I have to enter a amount of money and give the excess amount correctly. Example: if I enter 5.00 I should receive the answer 0.25.

when green flag clicked
ask [ Enter Name? ] and wait
set [ name ] to (answer)
ask [ enter amount ] and wait
set [ money ] to (answer)
if <(money) > [4.75]> then
set [ excess ] to ((4.75) - (money))
say (join (name) (join [paid too much by ] (excess))) for (2) secs

end
deck26
Scratcher
1000+ posts

rounding to nearest number?

OK, I think I understand what you mean.

First of all, the excess is money - 4.75 rather than 4.75 - money. Then the 0.25 you get when you enter 5 will be a positive value.

I'm guessing you want two digits after the decimal point rather than rounding.

If you multiply the value by 100, then round and then divide by 100 you will generally get this but you'd also have to add extra zeroes. If the returned value was something like 4.2 you'd want it to show as 4.20 I assume. Unfortunately Scratch doesn't give us the option of formatting numbers in this way so you have to do it yourself.

The first bit is easy

((round ((100) * (money)) ) - [475])  / [100]  // round possibly not required since 100*money is probably whole number
but for the second bit you need to take that value and work out how many 0s to add, if any.

One option
set [bigval v] to ((round ((100) * (money)) ) - [475])
set [newval v] to ((bigval) / [100])
repeat until <(length of (newval)) > (length of (bigval))>
set [newval v] to (join (newval) [0])
end
jacob_1986
New to Scratch
9 posts

rounding to nearest number?

I need the program to give the exact change, but I don't understand why the program gives the answer 0.970000000000000002 to the input 3.77 but then gives the answer 0.75 to the input 3.99? I know there is a math problem here regarding significant figures, but it's somehow controlling the output to the exact amount?
deck26
Scratcher
1000+ posts

rounding to nearest number?

jacob_1986 wrote:

I need the program to give the exact change, but I don't understand why the program gives the answer 0.970000000000000002 to the input 3.77 but then gives the answer 0.75 to the input 3.99? I know there is a math problem here regarding significant figures, but it's somehow controlling the output to the exact amount?
Because of the way computers store non-integer values which is not exact for all numbers. If you want to keep everything exact to 2 digits after the point you may actually be better multiplying everything by 100 and working with integers and dividing by 100 ONLY when displaying.

If interested you could start with reading the wikipedia page on Computer_number_format

Powered by DjangoBB

Standard | Mobile