duanjiyun7391 2017-10-26 14:31
浏览 19

在进行迁移时如何创建浮动而不是双重的字段?

I am using Laravel's migration system to recreate an existing database structure. The tables need to match the tables in this existing database exactly.

One of the fields on a table is described as so:

`frequency` float unsigned NOT NULL DEFAULT 0,

Looking at the Laravel migration documentation I see the following options:

$table->float('amount', 8, 2);
$table->double('column', 15, 8);

Both of these column types results in creating a double type column with a length and decimal points. But I need one that is specifically defined as a float with no specified length and zero decimal points.

It it possible using Laravel's migration system?

  • 写回答

1条回答 默认 最新

  • duanche9384 2017-10-29 14:35
    关注

    Do not use (m,n) on FLOAT or DOUBLE in MySQL. That feature belongs only on DECIMAL. If you want zero decimal places, use some form of INT. On the other hand, maybe your frequency is in the gigahertz? At which point FLOAT without precision or decimals, is the most appropriate.

    Note that 1GHz will not fit in FLOAT(8,2), but it will fit in FLOAT.

    Why both?... FLOAT takes 4 bytes; DOUBLE takes 8; both are part of the IEEE-754 standard that virtually every computer hardware implements. In the "old" days (when 1MB of RAM was a "big" machine; the standard dates back to the early '80s), saving 4 bytes was a big deal. If Laravel is turning float into double, that is its choice; MySQL will take either.

    In virtually all situations FLOAT and DOUBLE values and variables are interchangeable. But... A value stored in FLOAT and the same value stored in DOUBLE may lead to values that compare unequal. For example, float(1.234) != double(1.234) in virtually all languages, hardware, etc.

    MySQL is happy with frequency float unsigned NOT NULL DEFAULT 0.

    But, if $table->float('amount', 8, 2); in Laravel means the same as MySQL's FLOAT(8,2), then you are likely to have big problems.

    1234567.89 will be truncated to 999999.99. And 1.234 will be truncated to 1.23, then rounded to binary.

    Sounds like a bug report for Lavaral. (Sorry, I have never used Lavaral, so I am guessing at its details. I do know MySQL and IEEE-754.)

    评论

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景