Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Python floats

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?

Name: Anonymous 2007-02-28 8:46 ID:llMOMWu4

Opera can format strings in a webpage in any way you want.

www.opera.com

Name: Anonymous 2007-02-28 8:46 ID:fk8sAC/j

>>> x = 1.0
>>> int(x)
1

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?

Name: Anonymous 2007-02-28 12:16 ID:MuVy6YP9

>>4
x == (int)x ? (int)x : x;

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.

Name: Anonymous 2007-02-28 14:07 ID:LIWgyEHD

EPSILON

Name: Anonymous 2007-02-28 14:14 ID:oz390HEh

>>1
print "%g" % floatvar

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.

Name: Anonymous 2007-02-28 15:30 ID:VaBoIviY

Python is a sinker.

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.

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.

Name: Anonymous 2007-02-28 23:17 ID:LIWgyEHD

>>12

Mathematically, it is an integer

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

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.

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.

Name: Anonymous 2007-03-01 5:15 ID:APrkHKSY

FF = lambda f: int(f) if f == int(f) else f

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.

Name: Anonymous 2007-03-01 13:19 ID:NmkBTxji

You fuckers don't know the first thing about floating points

Name: Anonymous 2007-03-01 13:44 ID:Heaven

>>18
That should be directed at >>6, not >>14.

Name: Anonymous 2007-03-02 12:34 ID:ibD0gcSY

>>20

Chill the FUCK OUT

Name: Anonymous 2007-03-02 15:00 ID:L36f6HPy

ahahahaha habeeb it motherfuckers

Name: Anonymous 2010-11-14 1:13

Name: Anonymous 2012-03-23 23:40

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

Name: Anonymous 2012-03-25 21:43

>>1
Fucking nice post! Badabababa I'm lovin' it!

Name: Anonymous 2012-03-26 16:25

>>1
Hey /g/
lol wut

Name: Anonymous 2012-03-26 17:19

if number % 1 == 0.0:
    return int(number)
else:
    return number

Name: Anonymous 2012-03-26 17:32

>>27
welcome to /g/ niggertits newfriend

Name: Anonymous 2012-03-26 18:51

>>29
Piss off.

Name: Sgt.Kabu᪭玄kiman퓤妋 2012-05-28 22:48

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List