Forum

> > CS2D > Scripts > Round to 2 numbers after comma
Forums overviewCS2D overview Scripts overviewLog in to reply

English Round to 2 numbers after comma

13 replies
To the start Previous 1 Next To the start

old Round to 2 numbers after comma

Alistaire
User Off Offline

Quote
Spoiler >


Found a way; nvm ;3

1
string.format('%.2f', value)
edited 4×, last 20.04.12 07:44:19 am

old Re: Round to 2 numbers after comma

Snurq
BANNED Off Offline

Quote
1
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)

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.

old Re: Round to 2 numbers after comma

Avo
User Off Offline

Quote
1
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

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

old Re: Round to 2 numbers after comma

Snurq
BANNED Off Offline

Quote
user Alistaire has written
I've found a much easier way;

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..

old Re: Round to 2 numbers after comma

Flacko
User Off Offline

Quote
user Snurq has written
user Alistaire has written
I've found a much easier way;

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

old Re: Round to 2 numbers after comma

Jhony
User Off Offline

Quote
user Avo has written
1
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

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?
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview