Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WoW - rounding to two decimal places in Lua

I'm trying to display my total experience points as a percentage rounded to two decimals. I have come up with a solution, but its quite clunky. There has to be a better way.

Here is what I have:

local xpPercentage = (((UnitXP("player") / UnitXPMax("player"))*100))
-- 63.4587392473
local xpMantissa = xpPercentage - floor(xpPercentage)
-- .4587392473
local xpTwoDec = (floor(xpMantissa * 100)/100) + floor(xpPercentage)
-- 63.45

Again, this does what I want, but is there a prettier way to do it?

like image 295
BWayne Avatar asked Sep 16 '19 03:09

BWayne


People also ask

How to round values in Lua using math functions?

This is a sample example for beginners to start with rounding values in lua using math functions. print ("print result here ..") By using the round function, we can remove the decimal part of the numbers, it will return us the next whole number.

How do you round to thousandths place?

Now, look at the digit on the right side of the place value you wanted to round to i.e. thousandths place. In this case, it is 8 Since 8 > 5 we will round up and increase the hundredths place by 1 i.e. 5+1 =6. Ignore the remaining digits in the decimal number.

How to round a number to the number of decimal places?

The following function rounds a number to the given number of decimal places. function round (num, numDecimalPlaces) local mult = 10^ (numDecimalPlaces or 0) return math.floor (num * mult + 0.5) / mult end function round2 (num, numDecimalPlaces) return tonumber ( string.format ( "%."

How to round a number without a decimal in Python?

math.ceil (number): As you can see in the method signature, it takes one parameter inside the function, which represents the value to be round off. It will always return us the number without decimal. Let’s take a simple example : Points to be remembered while using round function in lua;


2 Answers

local formatted = string.format(
   "%.2f %%",
   UnitXP('player') / UnitXPMax('player') * 100
)

That's the standard Lua way to do it, which should work for you as well. Unless WoW has removed that function, which would be silly.

Note that type(formatted) is String, not a number.


string.format, as described in the manual, takes a format string as its first argument, followed by a series of values you want to splice into the format string.

The format string will mostly be treated literally, except special tokens that start with %. The number of additional arguments should be equal to the number of these tokens in the format string.

In the example above, %f means "insert a float here"; for example, string.format("hello %f world", 5.1") would return "hello 5.1 world". Adding stuff after the % and before the f you can tell it how exactly to format it.

Here's an example using all the options: string.format("%x6.2f", 2.264)

From left to right:

  • % marks the start
  • x tells it to pad right with xs
  • 6 tells it the whole thing should be 5 characters long
  • .2 tells it to round (or pad with 0s) to 2 decimal places

So, the result would be xx2.26

Finally, since % holds special meaning in the format string, if you want a literal % you have to write %% instead.

"%.2f %%" thus means:

A float, rounded or padded to 2 decimals, followed by a space and a percent sign. The second argument to format must then be a number, or the function will error.

like image 108
DarkWiiPlayer Avatar answered Oct 07 '22 10:10

DarkWiiPlayer


Here is a flexible function to round to different number of places. I tested it with negative numbers, big numbers, small numbers, and all manner of edge cases, and it is useful and reliable:

function Round(num, dp)
    --[[
    round a number to so-many decimal of places, which can be negative, 
    e.g. -1 places rounds to 10's,  
    
    examples
        173.2562 rounded to 0 dps is 173.0
        173.2562 rounded to 2 dps is 173.26
        173.2562 rounded to -1 dps is 170.0
    ]]--
    local mult = 10^(dp or 0)
    return math.floor(num * mult + 0.5)/mult
end
like image 42
George Williams Avatar answered Oct 07 '22 11:10

George Williams