以浮点数形式向下移动点

发布于 2024-10-27 02:16:16 字数 493 浏览 1 评论 0原文

首先,感谢您的关注,我是这个网站的新手 ^^ 所以如果我做错了什么请原谅...

我的 Python 代码有一个很大的问题...我是编程新手,而且我我也是 Python 新手。

我需要取一个浮点数并将该点向右移动,使其成为一个整数,例如取 60.27 并得到 6027。

我使用的算法是递归乘以 num*10 直到 num%2==0,然后获取int(num)

问题是,当我相乘(例如)602.47*10时,它返回6024.700000000001并且它显然不起作用:-)

有什么方法可以修复它,或者任何其他技术或其他方式可以递归地执行此操作?我可以使用我需要的任何东西,但它必须是递归的:没有 forwhile...

感谢您的帮助!我的母语不是英语,所以如果很难阅读,请原谅......

First off all, thanks for the attention, I'm new to this site ^^ so excuse me if I do something wrong...

I have a huge problem with my Python code... I'm new to programming, and I'm new to Python as well.

I need to take a floating point number and move the point right so it becomes an integer, like taking 60.27 and getting 6027.

The algorithm that I'm using is recursively multiplying num*10 until num%2==0, then getting the int(num).

The problem is, when I multiply (for example) 602.47*10 it returns 6024.700000000001 and it obviously doesn't work :-)

Is there any way to fix it, or any other technique or other way to do this recursively?? I'm allowed to use anything I need, but its got to be recursive: no for or while...

Thanks for the help!! My first language is not english, so I beg your pardon if it's hard to read...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如果没有 2024-11-03 02:16:16
>>> str(60.27).translate(None, '.')
'6027'

使用 lstrip('0') 防止小数低于 1。


来自文档:

S.translate(table [,deletechars]) ->字符串

返回字符串 S 的副本,其中出现所有字符
在可选参数中删除字符被删除,并且
剩余字符已通过给定映射
翻译表,必须是长度为256的字符串。

>>> str(60.27).translate(None, '.')
'6027'

Use lstrip('0') to guard against decimals below 1.


From the docs:

S.translate(table [,deletechars]) -> string

Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.

等数载,海棠开 2024-11-03 02:16:16

浮点表示有这个问题。

您是否想要更改:

1.2345
12.345
123.45
1234.5

全部更改为 12345?

对于没有精确表示的浮点数(您提到 6024.70),您是否期望得到 6024700000000001,因为这是可以存储在浮点数中的最接近 6024.70 的输出?

Floating point representations have that issue.

Are you looking to change:

1.2345
12.345
123.45
1234.5

all to 12345?

For floats which have no exact representation (you mention 6024.70), do you expect to get 6024700000000001, since that's the output of the closest thing to 6024.70 which can be stored in float?

浅暮の光 2024-11-03 02:16:16

您可以尝试以下操作:

x = 60.27
newx = int(str(x).replace('.',''))

编辑: 作为旁注,字符串 .replace.translate 对于各种大小的浮点数具有相似的性能

%timeit int(str(4.73285).replace('.',''))
100000 loops, best of 3: 2.65 us per loop

%timeit int(str(4.73285).translate(None, '.'))
100000 loops, best of 3: 3.02 us per loop

You could try something like:

x = 60.27
newx = int(str(x).replace('.',''))

Edit: as a side note, the string .replace and .translate have similar performance for various sized floats

%timeit int(str(4.73285).replace('.',''))
100000 loops, best of 3: 2.65 us per loop

%timeit int(str(4.73285).translate(None, '.'))
100000 loops, best of 3: 3.02 us per loop
清泪尽 2024-11-03 02:16:16

将数字解析为字符串并进行字符串操作的算法会更可靠。正如您所见,任何涉及浮点数的数值计算都必然不准确。这是没有办法解决的。

It would be more reliable an algorithm to just parse the number as a string and do a string manipulation. Any numerical calculation involving floating-point numbers are bound to inaccuracy, as you've witnessed. There's no going around that.

南七夏 2024-11-03 02:16:16

由于您无法(可靠地)使用浮点来完成您想要的操作,因此一个简单的技巧是将数字转换为字符串,然后去掉小数点:

int(str(num).replace('.',''))

这将适用于任何未用科学记数法表示的数字。如果您的数字足够大(或小),以至于最终以科学计数法表示,请查看 这个

Since you can't (reliably) use floating point to do what you want, an easy hack is to convert the number to a string then rip out the decimal point:

int(str(num).replace('.',''))

That will work with any number that isn't represented in scientific notation. If your numbers are big (or small) enough that they do end up represented in scientific notation, have a look at this.

找回味觉 2024-11-03 02:16:16

只是在黑暗中大胆尝试,但是您的数字是否代表您试图在(例如)美元和美分之间转换的金额?如果是这样,您需要停止正在做的事情并将所有内容转换为美分,并且在实际向用户展示内容时仅使用“美元”值。使用浮点数表示货币值是一个非常非常糟糕的主意。

如果没有,请忽略我:-)

Just taking a wild stab in the dark here, but do your numbers represent amounts of money that you're trying to convert between (say) dollars and cents? If so, you need to stop what you are doing and convert everything to cents, and only use "dollar" values when actually presenting things to the user. Using floating point numbers for money values is a very, very bad idea.

If not, ignore me :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文