Added math.Truncate( num, idp ) #804

Merged
merged 3 commits into from Sep 9, 2014

Projects

None yet

6 participants

@KrakenZ
Contributor
KrakenZ commented Sep 7, 2014

math.Round rounds to the nearest integer.
math.Truncate rounds towards zero.

I needed it for some equations and thought I'd make a pull request for others.
This is practically the same function as math.Round, except it doesn't add 0.5, which makes it not rounds to the nearest integer.

local num = 54.59874

print( math.Truncate( num, 2 ) ) -- 54.59
print( math.Round( num, 2 ) ) -- 54.6
KrakenZ added some commits Sep 7, 2014
@KrakenZ KrakenZ Added math.Truncate( num, idp )
math.Round rounds to the nearest integer.
math.Truncate rounds towards zero.

I needed it for some equations and thought I'd make a pull request for others.
This is practically the same function as math.Round, except it doesn't add 0.5, which makes it not rounds to the nearest integer.

```lua
local num = 54.59874

print( math.Truncate( num, 2 ) ) -- 54.59
print( math.Round( num, 2 ) ) -- 54.6
```
397ce2c
@KrakenZ KrakenZ Woops. Fixed for negative numbers 37dfea0
@KrakenZ KrakenZ I gotta sleep.
Fixed bad variables
019dfd0
@robotboy655
Collaborator

Should something so simple be included? Opinions? I mean all this does is

math.floor( 54.59874 * 100 ) / 100
@SimonSchick
Contributor

It's more complicated than that.

Even tho I don't see a use-case right now, there probably is application for it, should just throw it in.

@KrakenZ
Contributor
KrakenZ commented Sep 7, 2014

simple calculation, as math.Round.
for me it makes all the difference because this is accurate. 😃

@Grocel
Grocel commented Sep 8, 2014

It wouldn't hurt to have it.

@SpencerSharkey

I'd use it.

@Acecool
Contributor
Acecool commented Sep 9, 2014

We have math.Round which supports rounding up / down to nearest whole number, or x decimals out as second argument. We have math.ceil and math.floor too.

This cuts off the end, so it could have a use.

For me, math.Round( x, 2 ) would be better because the number is closer to number above than the number below.

@SimonSchick
Contributor

@Acecool you don't seem to understand, this function does not round, it truncates the number to the given amount of digits, that means that it does not touch the digits before the truncation point.

@robotboy655 robotboy655 merged commit 2197a37 into garrynewman:master Sep 9, 2014
@KrakenZ KrakenZ deleted the unknown repository branch Sep 9, 2014
@Acecool
Contributor
Acecool commented Sep 10, 2014

I understand what it does, I don't have a use for it, but others may as I said in my post.. [quote]This cuts off the end, so it could have a use. [/quote]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment