albertodev01 / fraction

A Dart package that helps you work with fractions.
https://pub.dev/packages/fraction
MIT License
19 stars 5 forks source link

String representation of whole numbers using MixedFraction #29

Closed mskogholt closed 1 day ago

mskogholt commented 4 days ago

Is your feature request related to a problem? Please describe. When using the toString() method of the MixedFraction there is a check whether $whole == 0 (and omits it if it is), which is great, except when using this on whole numbers the string representations becomes 'x 0/1', where x = the whole number.

Describe the solution you'd like I think it would be great to omit the fractional part in this case and simply return x.

I would think simply an added check such as this would suffice:

@override
String toString() {
  if (whole == 0) {
    return '$numerator/$denominator';
  }
  if (numerator == 0) {
    return '$whole';
  }

  return '$whole $numerator/$denominator';
}

Maybe the same for the toStringAsGlyph method

mskogholt commented 4 days ago

I opened a PR for this: https://github.com/albertodev01/fraction/pull/30

The toStringAsGlyph will throw an error in this case anyway since 0/1 is not a valid glyph, so I left that as is.

albertodev01 commented 1 day ago

Thanks for your PR!! This week I'll merge it and create a new release 🙂 Sorry for the late reply but I've been on vacations and I'll be back tomorrow