Round to 2 numbers after comma
13 replies
1

Whut.
OWait if I'm not wrong, but probably I am...
Try to use <wholespamnumber> % 0.10
OWait if I'm not wrong, but probably I am...
Try to use <wholespamnumber> % 0.10
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV --==-- Ask me something. http://ask.fm/faptis
Crystal Rain has written:Whut.
Useless comments are useless. He wants to round numbers to two after the comma.
@
Alistaire: No idea, but shouldn't "9213.92" be 9213.93 instead as you're rounding up. Code:
1
2
3
4
5
6
7
2
3
4
5
6
7
function round(num)
local a = math.floor(num*100 + 0.5)/ 100
return a,string.format("%.2f",a)
end
num,num_rep = round(9213.001)
print(num,num_rep)
local a = math.floor(num*100 + 0.5)/ 100
return a,string.format("%.2f",a)
end
num,num_rep = round(9213.001)
print(num,num_rep)
output:
9213 9213.00
here you go..
it depends on what you want as a return from the function, a string, use the string.format. a number, use the normal a.
Code:
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
function round(nmb,count)
if not count then count=2 end
local nmb2=math.floor(nmb)
local number=nmb-nmb2
number=nmb2*math.power(10,count)
number=math.floor(number)
number=number/math.power(10,count)
nmb2=nmb2+number
return nmb2
end
if not count then count=2 end
local nmb2=math.floor(nmb)
local number=nmb-nmb2
number=nmb2*math.power(10,count)
number=math.floor(number)
number=number/math.power(10,count)
nmb2=nmb2+number
return nmb2
end
i think it should work, else do it this way but change logical bugs(if there is any)
*EDITED*
edited 1×, last 19.04.12 10:29:39 pm
Alistaire has written:I've found a much easier way;
Code:
1
string.format('&.2f', value)
You mean I found it? because you just copied what i had.
My code rounds the number ( something you asked for) and plain %.2f just shortens the amount of digits..
Snurq has written:
Alistaire has written:I've found a much easier way;
Code:
1
string.format('&.2f', value)
You mean I found it? because you just copied what i had.
My code rounds the number ( something you asked for) and plain %.2f just shortens the amount of digits..
Oh for fuck's sake. You can find printf/string.format examples anywhere.
Re-re edit: string.format("%.2f",x) also rounds up if the next digit is >= 5
edited 4×, last 19.04.12 11:37:42 pm
Bolt has written:Code:
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
function round(nmb,count)
if not count then count=2 end
local nmb2=math.floor(nmb)
local number=nmb-nmb2
number=nmb2*math.power(10,count)
number=math.floor(number)
number=number/math.power(10,count)
nmb2=nmb2+number
return nmb2
end
if not count then count=2 end
local nmb2=math.floor(nmb)
local number=nmb-nmb2
number=nmb2*math.power(10,count)
number=math.floor(number)
number=number/math.power(10,count)
nmb2=nmb2+number
return nmb2
end
i think it should work, else do it this way but change logical bugs(if there is any)
*EDITED*
Are you aware that wrote shit?
Snurq has written:because you just copied what i had.
I've searched it around the internet and found it in the Lua Dictionary.
I didn't even notice your script.
1








Round to 2 numbers after comma


Unlocking the games.