Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom formatting of magnitude of a quantity #108

Open
maekl opened this issue Apr 16, 2020 · 1 comment
Open

Allow custom formatting of magnitude of a quantity #108

maekl opened this issue Apr 16, 2020 · 1 comment

Comments

@maekl
Copy link
Contributor

maekl commented Apr 16, 2020

It would be nice to be able to customise the formatting of the magnitude of a quantity.

Maybe by introducing ShowMagnitude similar to ShowUnit and ShowDimension.

trait ShowMagnitude[A] {
  def apply(a: A): String
}

Default instance could use toString or maybe even it makes sense to use DecimalFormat (with a couple of significant digits) as the default for Double and BigDecimal

@zainab-ali
Copy link
Contributor

Thanks for the suggestion @maekl !

Yes, I think we could benefit a lot from this typeclass.

The current Show implemenation isn't particularly principled. We're using an implicit toString in q.value:

    implicit def quantityShow[A, D <: HList](
        implicit showUnit: dimensions.ShowUnit[D],
        showDimension: dimensions.ShowDimension[D]
    ): Show[Quantity[A, D]] =
      new Show[Quantity[A, D]] {
        def apply(q: Quantity[A, D]): String =
          s"${q.value} ${showUnit().trim} [${showDimension().trim}]"
      }

It would be better if this used a show instance for magnitude instead.
Is this something you'd be willing to submit a PR for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants