2 posts
Posted 07 December 2013 - 04:47 PM
I am making a POS system(a cash register), and for the total, I want to keep the format like 5.00, instead of 5 if the total is $5. Basically, I need help keeping a number a "Float" or "Double" type instead of having lua convert it automatically to an integer if there are no "necessary" decimals.
8543 posts
Posted 07 December 2013 - 06:48 PM
Look into string.format. There are some parts of it that don't work well, but I think that the decimals formatting part does work.
7508 posts
Location
Australia
Posted 07 December 2013 - 07:21 PM
but I think that the decimals formatting part does work.
Unfortunately I've
never been able to get it to work, originally I thought it may have been 'cause of the difference between Lua and Java's
string.format, but upon further investigating they're identical in how they function, but its broken, this
string.format("%.2f", math.pi) outputs
3.141592653589793 which is clearly wrong.
As such you'd need to do something like this;
string.format("$%d.%.2d", dollars, cents) which when given input
string.format("$%d.%.2d", 2, 5) will output
$2.05.
The difference between these:
- adding the .2 in the standard floating point value format %f specifies that there needs to be two decimal places in a floating point value adding any trailing zeros where appropriate (unfortunately broken);
- adding the .2 in the standard double value format %d specifies that there needs to be a minimum of two digits in the double adding any leading zeros where appropriate. With the string.format a double does not preserve any decimal values and is cast to an integer. this can be seen by testing the following code string.format("%d", math.pi) and you'll notice that 3 is the result;
Edited on 07 December 2013 - 06:21 PM
756 posts
Posted 02 December 2014 - 11:19 AM
Sorry for bump.
There's a workaround to this problem
local function round(num, dec)
local shift = 10^(dec or 2)
return math.floor(num * shift + 0.5) / shift
end
round(math.pi) -> 3.14
round(math.pi, 4) -> 3.1416
3057 posts
Location
United States of America
Posted 02 December 2014 - 02:04 PM
Sorry for bump.
There's a workaround to this problem
local function round(num, dec)
local shift = 10^(dec or 2)
return math.floor(num * shift + 0.5) / shift
end
round(math.pi) -> 3.14
round(math.pi, 4) -> 3.1416
That's not the problem. He wants 4.00 to stay 4.00 and not automatically turn into 4.
756 posts
Posted 03 December 2014 - 12:50 PM
That was about the problem theoriginalbit stated.
But I guess we can do something like this instead.
local function round(num, dec)
local shift = 10^(dec or 2)
num = math.floor(num * shift + 0.5) / shift
if num == math.floor(num) then num = tostring(num.."."..("0"):rep(dec)) end
return num
end
7508 posts
Location
Australia
Posted 03 December 2014 - 01:17 PM
except then it returns as a string, instead of a number meaning it would no longer be an adequate round function
756 posts
Posted 03 December 2014 - 05:37 PM
Normal behavior you would expect with string.format using %.2f would also return a string, my function was pretty much a work around for that string.format bug.
You would use the result of that function mainly for display purpose, not actual calculations.
Edited on 03 December 2014 - 04:41 PM