【问题标题】:Why does "M" appear in Clojure MySQL Query Results为什么 Clojure MySQL 查询结果中出现“M”
【发布时间】:2014-09-08 23:03:45
【问题描述】:

我有一个返回行的 Clojure 查询,这里是返回行的部分打印输出(映射)。

({:employer_percent 0.00M, ... :premium 621.44M, ...})

这两列分别是mysql表中的decimal(5,2)和decimal(7,2)。

为什么每个值的末尾都有一个“M”后缀?这是形成和执行查询的代码。

    (def db {:classname "com.mysql.jdbc.Driver"
             :subprotocol "mysql"
             :subname "//system/app"
             :user "app-user"
             :password "pwrd"})

  (defn get-recent-gic-rows
  [search-date lt-toggle]
  (let [query (if (= 0 lt-toggle)
   (str "select g.* from gic_employees g where g.processed_date <= '" search-date "' order by g.processed_date desc limit 1  ")
   (str "select g.* from gic_employees g where g.processed_date <  '" search-date "' order by g.processed_date desc limit 1  "))]

        (j/query db
             [query])))

【问题讨论】:

    标签: mysql sql jdbc clojure


    【解决方案1】:

    M后缀表示号码为BigDecimal。你可以在 REPL 中检查这个

    user=> (class 1)
    java.lang.Long
    user=> (class 1.0)
    java.lang.Double
    user=> (class 1M)
    java.math.BigDecimal
    

    由于您的数据库列类型为decimal(5,2)decimal(7,2),因此将数字转换为floatdouble 是不安全的,因为这些浮点类型不能代表decimal(5,2) 或@ 的所有数字987654329@准确。

    您可以使用关键字“浮点不准确”进行谷歌搜索。 Stackoverflow 中也有大量文章和问答。

    【讨论】:

    • 感谢您的回答。我想我可以将该映射键的值转换为字符串,然后将 M 从末尾拉出。
    • @octopusgrabbus 没有什么可以阻止您将 Bi​​gDecimal 显式转换为 Double,例如通过 (double 1M) 只会产生 1.0 但对于其他值,您可能会失去精度。如果您的目标是在某处打印值,只需使用带有适当格式字符串的clojure.core/format(或clojure.core/printf),不要弄乱值本身。
    猜你喜欢
    • 1970-01-01
    • 2020-08-01
    • 2021-10-18
    • 2013-08-05
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多