简体   繁体   English

PostgreSQL数字类型和Play的anorm解析器! 斯卡拉2.3

[英]PostgreSQL numeric type and anorm parser with Play! scala 2.3

I want to use the numeric type of Postgresql 9.3 but my parser : 我想使用Postgresql 9.3的数字类型,但是我的解析器是:

private val Account403Parser: RowParser[Account403] = {
  get[Long]("id") ~
  get[Float]("amount") map {
  case id ~ amount =>
    Account403.apply(id, amount)
}

doesn't work since anorm can't convert from java.math.BigDecimal to Float. 无法正常运行,因为主动脉无法从java.math.BigDecimal转换为Float。

Here is the error that I get : 这是我得到的错误:

[RuntimeException: TypeDoesNotMatch(Cannot convert 9.5: class java.math.BigDecimal to Float for column ColumnName(account403.amount,Some(amount)))]

How can I change my parser to make it convert from the java.math.BigDecimal type to the Float type? 如何更改解析器以使其从java.math.BigDecimal类型转换为Float类型?

According to the type-compatibility matrix in the documentation you should use 根据文档中类型兼容性矩阵,应使用

BigDecimal , Double or Long to assign a BigDecimal . BigDecimalDoubleLong来分配BigDecimal

Or you could cast to real (4-byte floating point number) on the Postgres side: amount::real , thereby losing some precision. 或者,您可以在Postgres端将其强制转换为real (4字节浮点数): amount::real ,从而失去一些精度。 Not sure about the syntax in scala. 不确定scala中的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM