Skip to content

Struggling with parameterizing type level scale #5

@symbiont-alexey-babkin

Description

@symbiont-alexey-babkin

Hi, I'm trying to use the lib to round up decimals at different places past the decimal point without reducing the scale.
I.e. to have roundHalfUp that would take a decimal and an Integer that would specify at what place past the decimal point the decimal should be rounded.
e.g. (pseudocode)roundHalfUp 2.415 2 would return 2.42 (round at 2 places after the decimal point)
Code is looking something like this:

roundDecimalBy ::
  D.Round r UnderlyingIntegral =>
  KnownNat k =>
  KnownNat n =>
  Proxy k ->
  D.Decimal r (n + k) UnderlyingIntegral ->
  D.Decimal r n UnderlyingIntegral
roundDecimalBy _ d = D.roundDecimal d 

roundHalfUp :: MyDecimal -> Integer -> Either SomeException MyDecimal
roundHalfUp (MyDecimal d) (someNatVal . naturalFromInteger -> (SomeNat n)) =
  map MyDecimal . D.scaleUpBounded $ roundDecimalBy n d

basically I'm trying to drive the scale change between scaleUpBounded and roundDecimal with an Integer value.
The hardship seems to be that scale is at the type level, so I need some dependently typed trickery to get this to work.
Would appreciate guidance on how to make this work.
Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions