十进制 - 删除尾随的零和替补科学符号
我需要一个像format_float_postional一样起作用的函数,但没有圆形。它是函数:
- 应删除尾随的零
- 不是圆形的(问题),
- Scientifict表示法,
- 如果可以用十进制来使用
这是我发现的最近的灵魂: https://stackoverflow.com/a/58106536/3565923
import numpy as np
formated = np.format_float_positional((float(instance.factor)), trim="-")
intance因素是Serializer中的领域。这是小数!
class NewConversionSerializer(serializers.ModelSerializer):
factor = serializers.DecimalField(max_digits=40, decimal_places=20)
目前我得到: 0.55555555555555555550-> 0.55555555555556
我希望它是:0.5555555555555555555
如果输出和输入是十进制的,那将是很棒的。
I need a function which works like format_float_postional, but doesn't round. It is function which:
- should remove trailing zeros
- doesn't round (problem with that)
- should supress scientifict notation
- can be used with Decimal
This is the closesest soultion I found:
https://stackoverflow.com/a/58106536/3565923
import numpy as np
formated = np.format_float_positional((float(instance.factor)), trim="-")
Intance factor is a field in serializer. It is Decimal!
class NewConversionSerializer(serializers.ModelSerializer):
factor = serializers.DecimalField(max_digits=40, decimal_places=20)
At the moment I get:0.55555555555555555550 -> 0.5555555555555556
I'd want it to be: 0.5555555555555555555
It would be great if both output and input would be decimal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出:
它有效(Python 3.8.10),但我认为它需要更多的测试...
Output:
It works (Python 3.8.10), but it needs a lot more testing in my opinion...