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 |
|
|
| |
|
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 |
|
|
|
| 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
|
  |
| Joined: 30 Jun 2012 |
| Total Posts: 10044 |
|
|
| 27 Oct 2015 03:24 PM |
| Jimmy's way seems more efficient. |
|
|
| Report Abuse |
|
|
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
|
  |
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 27 Oct 2015 04:28 PM |
OR
function Round(n, place) return math.floor(Round/place+0.5)*place end |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| Joined: 16 May 2009 |
| Total Posts: 14084 |
|
|
| 27 Oct 2015 04:30 PM |
| I Already Won This Thread xD! Noob. |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| Joined: 30 Jun 2012 |
| Total Posts: 10044 |
|
| |
|
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
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|