This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
itsme's profile picture

[Lua] print.format() with floating point numbers

Started by itsme, 09 September 2012 - 06:53 PM
itsme #1
Posted 09 September 2012 - 08:53 PM
Hello!

If I enter the following expression (string.format("%.2f",123.2375)) in the Lua interactive prompt then the result is: 123.2375. It should be 123.24.

Is this an error?

Thanks
Lyqyd #2
Posted 09 September 2012 - 09:51 PM
The error is in your understanding of the string.format() function. The .2 specifies the minimum number of significant digits, not the maximum. This function may do what you want it to:


function round(num, places)
	num = tostring(num)
	local inc = false
	local decimal = string.find(num, "%.")
	if num:len() - decimal <= places then return tonumber(num) end --already rounded, nothing to do.
	local digit = tonumber(num:sub(decimal + places + 1))
	num = num:sub(1, decimal + places)
	if digit <= 4 then return tonumber(num) end --no incrementation needed, return truncated number
	local newNum = ""
	for i=num:len(), 1, -1 do
		digit = tonumber(num:sub(i))
		if digit == 9 then
			if i > 1 then
				newNum = "0"..newNum
			else
				newNum = "10"..newNum
			end
		elseif digit == nil then
			newNum = "."..newNum
		else
			if i > 1 then
				newNum = num:sub(1,i-1)..(digit + 1)..newNum
			else
				newNum = (digit + 1)..newNum
			end
			return tonumber(newNum) --No more 9s found, so we are done incrementing. Copy remaining digits, then return number.
		end
	end
	return tonumber(newNum)
end

Edit: Playing around with this a bit more seems to indicate a limitation around 8 significant digits.
itsme #3
Posted 09 September 2012 - 10:46 PM
Thank you very much for your reply and the function!
MysticT #4
Posted 10 September 2012 - 12:28 AM
The error is in your understanding of the string.format() function. The .2 specifies the minimum number of significant digits, not the maximum.
That's weird, according to the lua manual:
The format string follows the same rules as the printf family of standard C functions. The only differences are that the options/modifiers *, l, L, n, p, and h are not supported and that there is an extra option, q.
and in printf, for e, E and f specifiers, the .[number] (wich indicates the precision) is the number of digits to be printed after the decimal point.
Anyway, seems like LuaJ doesn't do it that way :D/>/>
Lyqyd #5
Posted 10 September 2012 - 01:23 AM
The error is in your understanding of the string.format() function. The .2 specifies the minimum number of significant digits, not the maximum.
That's weird, according to the lua manual:
The format string follows the same rules as the printf family of standard C functions. The only differences are that the options/modifiers *, l, L, n, p, and h are not supported and that there is an extra option, q.
and in printf, for e, E and f specifiers, the .[number] (wich indicates the precision) is the number of digits to be printed after the decimal point.
Anyway, seems like LuaJ doesn't do it that way :D/>/>

Ah, you are correct there it seems. It appears I had misread it. My apologies. Either way, none of the words minimum, maximum or exactly appear in the description, so as long as at least that many digits are shown, the implement technically matches all the documentation I can find. Also, it appears that it wouldn't make attempts to round off at the truncation point, which did seem to be the intent of the original question.