如何将烧瓶 - 塞拉尔奇米浮子限制为2个小数?

发布于 2025-02-08 21:38:40 字数 578 浏览 2 评论 0原文

我有这个模型,

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 技术交流群。

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

发布评论

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

评论(1

温柔嚣张 2025-02-15 21:38:40

如果您想要固定的精度/比例,您可能正在寻找 sqlalchemy 数字 type 而不是float。浮子不带有精度或比例信息。

microsoft-inosted 文档将精度和比例定义为:

精度是数字中的数字数。比例是数字中小数点右侧的数字数。例如,数字123.45的精度为5,比例为2。

例如,数字123.45的

      total = db.Column(db.Numeric(precision=10, scale=2), nullable=False)

If you want a fixed precision / scale, you're probably looking for the sqlalchemy Numeric type instead of Float. Floats don't carry precision or scale information.

This Microsoft-hosted document defines precision and scale as:

Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2.

So, the total column definition could be defined in sqlalchemy as

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