Why does “M” appear in Clojure MySQL Query Results

梦想与她 提交于 2021-02-08 05:38:14

问题


I have a Clojure query that returns a row, and here is a partial printout of the returned row (map).

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

These two columns are decimal(5,2) and decimal(7,2) respectively in the mysql table.

Why is there an 'M' suffix at the end of each of these values? Here is the code that forms and executes the query.

    (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])))

回答1:


M suffix means that the number is BigDecimal. You can check this in REPL

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

Since your database column type is decimal(5,2) and decimal(7,2), it's not safe to convert the numbers to float or double because those floating point type can't represent all the numbers of decimal(5,2) or decimal(7,2) accurately.

You can google with the keyword "floating point inaccuracy". There are tons of articles and Q&As, also within Stackoverflow.



来源:https://stackoverflow.com/questions/24814317/why-does-m-appear-in-clojure-mysql-query-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!