English Round to 2 numbers after comma

13 replies
Goto Page
To the start Previous 1 Next To the start
Up
Alistaire
User
Offline Off
Spoiler >


Found a way; nvm ;3

Code:
1
string.format('%.2f', value)
edited 4×, last 20.04.12 07:44:19 am
IMG:http://i.imgur.com/5zhwOTP.png
19.04.12 10:11:06 pm
Up
Infinite Rain
User
Offline Off
Whut.

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
19.04.12 10:13:01 pm
Up
Yates
Idiot
Offline Off
user Infinite Rain has written:
Whut.

Useless comments are useless. He wants to round numbers to two after the comma.

@user Alistaire: No idea, but shouldn't "9213.92" be 9213.93 instead as you're rounding up.
Signature was removed because it's apparently offensive (even though it was just a quote and has no offensive, irritating or flame content √).
19.04.12 10:14:05 pm
Up
Alistaire
User
Offline Off
Didn't notice since I was spamming it.
IMG:http://i.imgur.com/5zhwOTP.png
19.04.12 10:14:33 pm
Up
Avobolt
User
Offline Off
You mean 9213.928~9213.3 ??
19.04.12 10:15:52 pm
Up
Alistaire
User
Offline Off
user Avobolt has written:
You mean 9213.928~9213.3 ??


No. I mean I want a function.
IMG:http://i.imgur.com/5zhwOTP.png
19.04.12 10:26:53 pm
Up
Snurq
BANNED
Offline Off
Code:
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.
19.04.12 10:28:11 pm
Up
Avobolt
User
Offline Off
Code:
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
19.04.12 10:29:14 pm
Up
Alistaire
User
Offline Off
I've found a much easier way;

Code:
1
string.format('&.2f', value)
IMG:http://i.imgur.com/5zhwOTP.png
19.04.12 10:37:05 pm
Up
Snurq
BANNED
Offline Off
user 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..
19.04.12 10:41:50 pm
Up
Flacko
User
Offline Off
user Snurq has written:
user 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
19.04.12 10:48:58 pm
Up
Jhony
User
Offline Off
user Avobolt has written:
Code:
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?
19.04.12 11:05:57 pm
Up
Tobey
User
Offline Off
user Yates has written:
@user Alistaire: No idea, but shouldn't "9213.92" be 9213.93 instead as you're rounding up.


math.floor always rounds down.
signature too huge
20.04.12 07:42:45 am
Up
Alistaire
User
Offline Off
user 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.
IMG:http://i.imgur.com/5zhwOTP.png
To the start Previous 1 Next To the start