Last active
February 3, 2026 20:21
-
-
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/945930b1151f89f44229f3cafd355409262078f4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : zio-json - dealing with bigdecimal representation | |
| // keywords : scala, zio, zio-json, @testable, bigdecimal, issue, @fail | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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.4.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