Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How round DOWN a number?

Asked by 10 years ago

I have a small problem. I want to make a currency converter, but can't figure out how to tackle this problem. Example below:

WhatIHave = 55

WhatIGet = WhatIHave/10
--Output: 5.5

How do I round down WhatIGet to the lowest whole number (in this case, 5). I don't want to round it up.

3 answers

Log in to vote
5
Answered by
Destrings 406 Moderation Voter
10 years ago
Edited 6 years ago

You use math.floor

a = 5.5
print(math.floor(a))

And coltn5000, that is just long, you get the same result with

function round(n)
    return math.floor(n+.5)
end

print(round(5.6), round(5.4))

5 6

1
I downrated it? Nope, someone else. I can't downrate... I need atleast 5 reputation to downrate. I have -9... fahmisack123 385 — 10y
1
Just to let you know he said he wanted to make a converter I made an example so to say I also made a round function that way depending on where the decimal is he probably asked to lower it because he didn't know how to round it DragonSkyye 517 — 10y
1
I know how to round in real, but not on ROBLOX, fahmisack123 385 — 10y
1
Just to let you know he said he wanted to make a converter I made an example so to say I also made a round function that way depending on where the decimal is he probably asked to lower it because he didn't know how to round it DragonSkyye 517 — 10y
View all comments (11 more)
1
Also, wouldn't the output say 6 5 instead of 5 6? because umm, 5.6+0.5 = 6.1 so round(6.1) = 6??? fahmisack123 385 — 10y
1
Just to let you know he said he wanted to make a converter I made an example so to say I also made a round function that way depending on where the decimal is he probably asked to lower it because he didn't know how to round it DragonSkyye 517 — 10y
1
No math.ceil takes it up it doesn't add the decimal also I meant roblox not real life DragonSkyye 517 — 10y
1
No math.ceil takes it up it doesn't add the decimal also I meant roblox not real life DragonSkyye 517 — 10y
1
Mind deleting the duplicates? You're posting duplicate comments apparently... fahmisack123 385 — 10y
1
@lomb How do I replace `n` with `WhatIHave`?? It's a little confusing... fahmisack123 385 — 10y
1
n is the parameter. You call the function like round(WhatIHave). But if you want to only round down use math.floor(WhatIHave) Destrings 406 — 10y
1
So would this work?: while true do wait() math.floor(WhatIHave) end I did an infinite loop because if you type once, hit enter then try again, it won't round... fahmisack123 385 — 10y
1
It would work, but you are not doing anything with the rounded down value Destrings 406 — 10y
1
Mine is downvoted. Why? it's a simple and objective answer. Tesouro 407 — 10y
1
@lomb, What's your ROBLOX username, I have to credit you. @Tes, Idk, I can't downvote even though it's my question... fahmisack123 385 — 10y
Ad
Log in to vote
-1
Answered by
Tesouro 407 Moderation Voter
10 years ago
 WhatIHave = 55

WhatIGet = math.floor(WhatIHave/10)
--Output: 5

math.floor will round it down. (more info)

0
Why the -1? Tesouro 407 — 10y
0
math.floor() will round down by itself. why divide by 10? how does 55 round down to 5? H4X0MSYT 536 — 7y
Log in to vote
-4
Answered by 10 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Do this

function Round(value)
 if (value%1 > .5) then
   return math.ceil(value) -- rounds the value up
 else
  return math.floor(value) -- rounds the value down
 end
end


function Convert(Amount, ReachAmount) -- the amount, and max amount
  if (Amount >= ReachAmount) then
   Amount = Amount - ReachAmount -- this will be the amount the person gets if reached to the reach amount
  return Round(Amount)
  end
end

local newAmount = Convert(WhatIHave, 10)
print(newAmount) -- print transaction to the output
0
This will convert and round the amount. Please up vote my answer DragonSkyye 517 — 10y

Answer this question