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

inBestUnit #443

Open
Lasering opened this issue Jan 19, 2021 · 2 comments
Open

inBestUnit #443

Lasering opened this issue Jan 19, 2021 · 2 comments

Comments

@Lasering
Copy link
Contributor

Quantity could have a method toCoarsest like the one in Duration.

Return duration which is equal to this duration but with a coarsest Unit, or self in case it is already the coarsest Unit

Examples:

Duration(60, MINUTES).toCoarsest // Duration(1, HOURS)
Duration(1000, MILLISECONDS).toCoarsest // Duration(1, SECONDS)
Duration(48, HOURS).toCoarsest // Duration(2, DAYS)
Duration(5, SECONDS).toCoarsest // Duration(5, SECONDS)

But since quantities are doubles the toCoarsest would also do:

import squants.information.Information
import squants.information.InformationConversions._

900.gibibytes.toCoarsest // 0.9 Tebibytes

Why? Because to show information to an user the later is more user friendly

@Lasering
Copy link
Contributor Author

Lasering commented Jul 15, 2022

A very similar thing used to exist in DefaultFormatter. Adapted it to work with the latest Squants version:

given [Q <: Quantity[Q]]: Ordering[UnitOfMeasure[Q]] = (x: UnitOfMeasure[Q], y: UnitOfMeasure[Q]) =>
  val siUnit = x(1).dimension.siUnit
  val xSI = x(1).to(siUnit)
  val ySI = y(1).to(siUnit)
  xSI.compare(ySI)

extension [Q <: Quantity[Q]](quantity: Quantity[Q])
  def inBestUnit(units: TreeSet[UnitOfMeasure[Q]] = quantity.dimension.units.to(TreeSet)): Q =
    val unit = units
      .takeWhile { u => quantity.to(u).abs >= 1.0 }
      .lastOption
      .getOrElse(units.head)
    quantity.in(unit)

@Lasering Lasering changed the title toCoarsest inBestUnit Oct 6, 2022
@Lasering
Copy link
Contributor Author

In this something that would be merged? I can do a PR.

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

No branches or pull requests

2 participants