Python floats
1
Name:
Anonymous
2007-02-28 8:06
ID:hNUN74R6
Hey /g/, how do I display a float without ".0" at the end when its value is an integer? Is there a string formatting thing to do this?
2
Name:
Anonymous
2007-02-28 8:46
ID:llMOMWu4
Opera can format strings in a webpage in any way you want.
www.opera.com
3
Name:
Anonymous
2007-02-28 8:46
ID:fk8sAC/j
>>> x = 1.0
>>> int(x)
1
4
Name:
op
2007-02-28 8:57
ID:ZgcT/9vQ
>>3
Thanks, but I want it to display decimals if the number has any. I realize I could do something along the lines of `x = int(x) if int(x) == x else x`, but I was thinking there might be a better way?
5
Name:
Anonymous
2007-02-28 12:16
ID:MuVy6YP9
>>4
x == (int)x ? (int)x : x;
6
Name:
Anonymous
2007-02-28 13:08
ID:EofLCMMQ
Oh boy. Neither of you have used floats for anything serious.
Don't use equivalence when dealing with floats. Ever. Check that the difference is sufficiently small instead.
7
Name:
Anonymous
2007-02-28 14:07
ID:LIWgyEHD
EPSILON
8
Name:
Anonymous
2007-02-28 14:14
ID:oz390HEh
>>1
print "%g" % floatvar
9
Name:
Anonymous
2007-02-28 14:16
ID:Heaven
>>5
Apparently you missed the "Python" part.
>>6
That's what I meant with "something along the lines of". I am well aware of the problems with binary representations of floats.
10
Name:
Anonymous
2007-02-28 15:30
ID:VaBoIviY
Python is a sinker.
11
Name:
Anonymous
2007-02-28 16:43
ID:bgbSb0ad
I am well aware of the problems with binary representations of floats.
Oh, okay. Carry on then, good sir.
12
Name:
Anonymous
2007-02-28 19:40
ID:Heaven
>>6
Hey /g/, how do I display a float without ".0" at the end when its value is an integer?
It's not exactly an integer then.
13
Name:
Anonymous
2007-02-28 23:17
ID:LIWgyEHD
>>12
Mathematically, it is an integer
14
Name:
Anonymous
2007-03-01 0:25
ID:Heaven
>>13
Not if it is only "within epsilon" of the integer. It is an integer when
x == (int)x
15
Name:
Anonymous
2007-03-01 2:44
ID:1bc36AS5
To answer the OP's question, truncate or round the floating point value to an integer first. Then convert the integer to a string. Simple!
Except sometimes languages do the rounding or truncation implicitly. That gets in my hair, it does.
16
Name:
Anonymous
2007-03-01 3:11
ID:Heaven
>>15
Are you fucking stupid? The OP only wants the float truncated when it is an integer, and left alone when it isn't. Try reading the thread next time.
17
Name:
Anonymous
2007-03-01 5:15
ID:APrkHKSY
FF = lambda f: int(f) if f == int(f) else f
18
Name:
Anonymous
2007-03-01 6:23
ID:NmkBTxji
>>14
All integers the width of the mantissa can be represented exactly. And IEEE-754 normalisation rules mean that they will be. So a float can precisely represent integers from -2^24 to 2^24 and a double from -2^53 to 2^53.
So he can safely rely on the test x == (int)x for those ranges.
19
Name:
Anonymous
2007-03-01 13:19
ID:NmkBTxji
You fuckers don't know the first thing about floating points
20
Name:
Anonymous
2007-03-01 13:44
ID:Heaven
>>18
That should be directed at
>>6, not
>>14 .
21
Name:
Anonymous
2007-03-02 12:34
ID:ibD0gcSY
22
Name:
Anonymous
2007-03-02 15:00
ID:L36f6HPy
ahahahaha habeeb it motherfuckers
24
Name:
Anonymous
2010-11-14 1:13