如何将烧瓶 - 塞拉尔奇米浮子限制为2个小数?
我有这个模型,
class AccountsModel(db.Model):
accountId = db.Column(db.String(20), primary_key=True)
name = db.Column(db.String(20), nullable=False)
surname = db.Column(db.String(20), nullable=False)
total = db.Column(db.Float, nullable=False)
我希望我的数据库具有“总数”数字,其中有2个小
数(例如1.23工作),但是1.2停留1.2和1变为1.0,
我尝试过:
total = db.Column(db.Float(precision=2), nullable=False)
total = db.Column(db.Float(2), nullable=False)
total = db.Column(db.Float(5,2), nullable=False)
我也尝试过数字而不是float,但它会破坏所有内容。
I have this model
class AccountsModel(db.Model):
accountId = db.Column(db.String(20), primary_key=True)
name = db.Column(db.String(20), nullable=False)
surname = db.Column(db.String(20), nullable=False)
total = db.Column(db.Float, nullable=False)
I want my database to have "total" numbers with 2 decimals like 0.00
Numbers like 1.23 work, but 1.2 stays 1.2 and 1 becomes 1.0
I've tried:
total = db.Column(db.Float(precision=2), nullable=False)
total = db.Column(db.Float(2), nullable=False)
total = db.Column(db.Float(5,2), nullable=False)
I also tried Numeric instead of Float, but it breaks everything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要固定的精度/比例,您可能正在寻找 sqlalchemy
数字
type 而不是float
。浮子不带有精度或比例信息。此 microsoft-inosted 文档将精度和比例定义为:
例如,数字123.45的
If you want a fixed precision / scale, you're probably looking for the sqlalchemy
Numeric
type instead ofFloat
. Floats don't carry precision or scale information.This Microsoft-hosted document defines precision and scale as:
So, the
total
column definition could be defined in sqlalchemy as