Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 11, 2023 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/fbab108598c01df7e7622e95928ea252 to your computer and use it in GitHub Desktop.
Save dacr/fbab108598c01df7e7622e95928ea252 to your computer and use it in GitHub Desktop.
zio-json - dealing with bigdecimal representation / published by https://github.com/dacr/code-examples-manager #9d4116ca-6856-455c-9c3d-8a0f0d1facbd/5e82cd5cda6154fd2fda1b8d0bf151c94ac8eb7
// summary : zio-json - dealing with bigdecimal representation
// keywords : scala, zio, zio-json, @testable, bigdecimal, issue, @fail
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 9d4116ca-6856-455c-9c3d-8a0f0d1facbd
// created-on : 2022-07-02T08:45:35+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.2.2"
//> using dep "dev.zio::zio-json:0.5.0"
// ---------------------
// https://github.com/zio/zio-json/issues/644
// https://github.com/scala/bug/issues/9670
import zio.*
import zio.json.*
import zio.json.ast.Json.Num
import java.math.BigDecimal as JBigDecimal
import scala.math.BigDecimal as SBigDecimal
assert(SBigDecimal(42.0) == SBigDecimal(42))
assert(JBigDecimal(42.0) == JBigDecimal(42))
assert(SBigDecimal("42.0") == SBigDecimal("42"))
assert(JBigDecimal("42.0") != JBigDecimal("42"))
assert(JBigDecimal("42.0").stripTrailingZeros() == JBigDecimal("42"))
assert(JBigDecimal("420.00") != JBigDecimal("420.0"))
assert("42".fromJson[Num].toOption.get.value == JBigDecimal(42))
assert("42.0".fromJson[Num].toOption.get.value != JBigDecimal(42))
assert("42.0".fromJson[Num].toOption.get.value != JBigDecimal(42.0))
assert("42.0".fromJson[Num].toOption.get.value.stripTrailingZeros() == JBigDecimal(42)) // fix proposal - it makes sense
assert("42.0".fromJson[Num].toOption.get.value.stripTrailingZeros() == JBigDecimal(42.0)) // fix proposal - it makes sense
assert("42.0".fromJson[Num].toOption.get.value.stripTrailingZeros() != JBigDecimal("42.0")) // but keep in mind that of course
assert(JBigDecimal("420.0").stripTrailingZeros != JBigDecimal(420.0)) // Because it is a power of 10 !
assert(JBigDecimal("42.0").stripTrailingZeros == JBigDecimal(42.0)) // Because it is not a power of 10
assert(JBigDecimal("420.0").stripTrailingZeros == JBigDecimal("4.2E+2"))
assert(JBigDecimal("420.0").stripTrailingZeros.toString == "4.2E+2")
assert(JBigDecimal("420.0").setScale(1) != JBigDecimal(420.0))
assert(JBigDecimal("420.0").setScale(1).toString == "420.0")
assert(JBigDecimal("420.0").setScale(0).toString == "420")
assert("42.0".fromJson[SBigDecimal].toOption.get == SBigDecimal("42"))
assert("42.0".fromJson[SBigDecimal].toOption.get == SBigDecimal("42"))
println("JBigDecimal is also a big mess, and I would say that equality is based on the rendered form of the value, and not only its intrinsic value")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment