generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
We use cookies to offer you a better experience. By using Roblox.com, you are agreeing to our Privacy and Cookie Policy.
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How would I round a decimal variable to the nearest tenth?

Previous Thread :: Next Thread 
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
27 Oct 2015 03:14 PM
I am currently making a game, but for this game to be how I want it, I have to add/subtract decimal numbers. I know how math.floor works, but that only rounds it to the nearest integer. Any ideas?


When life gives you lemons, don't make lemonade!
Report Abuse
Happywalker is not online. Happywalker
Joined: 05 Mar 2010
Total Posts: 3672
27 Oct 2015 03:17 PM
x 10

math.floor / ceil

/ 10
Report Abuse
HexC3D is not online. HexC3D
Joined: 30 Jun 2012
Total Posts: 10044
27 Oct 2015 03:21 PM
Something like..

local x = tostring(x)

local ret = sting.find(x,".")
loca found = sting.sub(x, ret)

You can do it on from there.
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
27 Oct 2015 03:22 PM
local round = function(number, decp)
return tonumber(("%." .. decp .. "f"):format(number));
end

print(round(10.535, 2));
-- 10.54
Report Abuse
HexC3D is not online. HexC3D
Joined: 30 Jun 2012
Total Posts: 10044
27 Oct 2015 03:24 PM
Jimmy's way seems more efficient.
Report Abuse
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
27 Oct 2015 03:30 PM
The problem is that when adding/subtracting tenths, there seems to be a strange bug in the source code that adds a whole bunch of places.

Here is what it looks like. I can't explain it.

/WlZDApk


When life gives you lemons, don't make lemonade!
Report Abuse
rayk999 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
27 Oct 2015 03:32 PM
really guys

local newnum = math.floor(num*10)/10


Red Blossoms
Report Abuse
HexC3D is not online. HexC3D
Joined: 30 Jun 2012
Total Posts: 10044
27 Oct 2015 04:12 PM
i thought he was lookin for how to find the decimals..
Report Abuse
goro7 is not online. goro7
Joined: 01 Jul 2009
Total Posts: 735
27 Oct 2015 04:17 PM
function roundToTenth(x)
return math.floor(x*10 + .5)/10
end
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Oct 2015 04:21 PM
"The problem is that when adding/subtracting tenths, there seems to be a strange bug in the source code that adds a whole bunch of places. "

Floating Point Imprecision.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Oct 2015 04:22 PM
"I know how math.floor works, but that only rounds it to the nearest integer."

Apparently you don't. It rounds DOWN.
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
27 Oct 2015 04:25 PM
local function roundPlace(num,place)
return math.floor(num*10^place+0.5)/10^place
end

print(roundPlace(78.4781,0))
print(roundPlace(78.4781,1))
print(roundPlace(78.4781,2))
print(roundPlace(78.4781,4))



Instance.new("BodyVelocity",SenseiWarrior).velocity = CFrame.new(SenseiWarrior.Torso.Position,YourGirlsDMs.Position).lookVector * 10
Report Abuse
robotmega is not online. robotmega
Joined: 16 May 2009
Total Posts: 14084
27 Oct 2015 04:27 PM
The following function rounds a number to the given number of decimal places.
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
Here is an alternative implementation:

function round2(num, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
Both are Lua 5.0 and 5.1 compatible.

If the number is rounded in order to be printed, then remove the tonumber: Converting to number then back to string would reintroduce rounding errors.

Tests:

> function test(a, b) print(round(a,b), round2(a,b)) end
> test(43245325.9995, 3)
43245326 43245325.999
> test(43245325.9994, 3)
43245325.999 43245325.999
> test(43245325.5543654)
43245326 43245326
> test(43245325.5543654, 3)
43245325.554 43245325.554
> test(43245325.5543654, 4)
43245325.5544 43245325.5544
Note: The first function will misbehave if idp is negative, so this version might be more robust (Robert Jay Gould)
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Oct 2015 04:28 PM
OR


function Round(n, place)
return math.floor(Round/place+0.5)*place
end
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
27 Oct 2015 04:29 PM
function Round(n, place)
return math.floor(n/place+0.5)*place
end



Slight fail in previous script :P
Report Abuse
robotmega is not online. robotmega
Joined: 16 May 2009
Total Posts: 14084
27 Oct 2015 04:30 PM
I Already Won This Thread xD! Noob.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
27 Oct 2015 04:47 PM
MY favorite by far is Jimmy's way, but it actually has a slight problem with it. Here is the correction.

local round = function(number, decp)
return tonumber(("%.0" .. decp .. "f"):format(number));
end
Report Abuse
HexC3D is not online. HexC3D
Joined: 30 Jun 2012
Total Posts: 10044
27 Oct 2015 04:52 PM
But it's very slight,
Report Abuse
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
27 Oct 2015 05:54 PM
Thanks for the help!


When life gives you lemons, don't make lemonade!
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
27 Oct 2015 06:00 PM
rayk's way or out
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image