Forum

> > CS2D > Scripts > Round to 2 numbers after comma
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Round to 2 numbers after comma

13 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Round to 2 numbers after comma

Alistaire
User Off Offline

Zitieren
Spoiler >


Found a way; nvm ;3

1
string.format('%.2f', value)
4× editiert, zuletzt 20.04.12 07:44:19

alt Re: Round to 2 numbers after comma

Snurq
BANNED Off Offline

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

alt Re: Round to 2 numbers after comma

Avo
User Off Offline

Zitieren
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*
1× editiert, zuletzt 19.04.12 22:29:39

alt Re: Round to 2 numbers after comma

Snurq
BANNED Off Offline

Zitieren
user Alistaire hat geschrieben
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..

alt Re: Round to 2 numbers after comma

Flacko
User Off Offline

Zitieren
user Snurq hat geschrieben
user Alistaire hat geschrieben
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
4× editiert, zuletzt 19.04.12 23:37:42

alt Re: Round to 2 numbers after comma

Jhony
User Off Offline

Zitieren
user Avo hat geschrieben
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?
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht