How to convert float to sortable integer in Python

Larry Bates larry.bates at websafe.com
Mon Apr 30 09:23:27 EDT 2007


ireyes at javeriana.edu.co wrote:
> DEAR SIR,
> I SAW YOUR INTERNET QUESTION AND I HAVE THE SAME TROUBLE.
> CUOLD YOU HELP ME TO MAKE A FLOAT TO INTEGER CONVERTION?
> DO YOU HAVE ANY EXEL FILE THAT CAN DO THAT?
> REGARDS AND THANKS A LOT
> 
> IVAN REYES
> 
> ___________________________________
> 
> AVISO LEGAL: El presente correo electronico no representa la opinion o el consentimiento oficial de la PONTIFICIA UNIVERSIDAD JAVERIANA. Este mensaje es confidencial y puede contener informacion privilegiada la cual no puede ser usada ni divulgada a personas distintas de su destinatario. Esta prohibida la retencion, grabacion, utilizacion, aprovechamiento o divulgacion con cualquier proposito. Si por error recibe este mensaje, por favor destruya su contenido y avise a su remitente.
> En este aviso legal se omiten intencionalmente las tildes.
> 
> Este mensaje ha sido revisado por un sistema antivirus, por lo que su contenido esta libre de virus.
> This e-mail has been scanned by an antivirus system, so its contents is free of viruses.

You have given us an ill defined problem.

1) If you want to convert float to int just use int() function.
   s=int(f)

2) If you want to keep some fractional part of the float multiply
it by 10, 100, 1000, 10000, ... depending on how many digits of
precision you wish to retain and then take int().

   s=int(f*10000)

would retain 4 digits of precision and throw away the remainder.

3) You can sort on float just fine, so why do you need int()?

-Larry



More information about the Python-list mailing list