蟒蛇从一个浮动至少显著位数(不使用字符串操作) [英] Python get least significant digits from a float (without using string operations)

查看:189
本文介绍了蟒蛇从一个浮动至少显著位数(不使用字符串操作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有浮动12345.6789我想要得到的六个最低显著数字(即45.6789)为int(即456789)使用Python中位运算(诉2.6)。

我该怎么办呢?

感谢

PS我不希望使用的字符串操作,即使它是相当容易:任何浮动F:

  INT(STR(INT(F * 1000))[ -  10:])

编辑:这原来的问题是毫无意义的,如图中的注释。许多道歉......而不是在得到不使用字符串至少显著数字方法如下所示(使用int和模量)


解决方案

 >>> A = 12345.6789
>>> B = INT(A * 10000)
>>> b
123456789
>>> C = B%百万
>>> C
456789

可是 - 为什么?

Assuming I have the float 12345.6789 and I want to get the six least significant digits (i.e. 45.6789) as an int (i.e. 456789) using bit operations in python (v. 2.6).

How do I do that?

Thanks

PS I do not want to use string operations even if it would be rather easy to: for any float f:

int(str(int(f * 1000))[-10:])

EDIT: This original question is pointless, as shown by comments within. Many apologies... instead methods on getting the least significant digits without using strings are shown below (using ints and modulus)

解决方案

>>> a = 12345.6789
>>> b = int(a*10000)
>>> b
123456789
>>> c = b % 1000000
>>> c
456789

But - WHY??

这篇关于蟒蛇从一个浮动至少显著位数(不使用字符串操作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆