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

Implement DecimalNotationFormatter and associated tests #36

Merged
merged 9 commits into from
Apr 17, 2024

Conversation

lipchev
Copy link
Contributor

@lipchev lipchev commented Apr 12, 2024

Description:

  • Added a new DecimalNotationFormatter class for formatting Fraction objects into decimal string representations corresponding to the Standard numeric format strings.
  • Created tests for the DecimalNotationFormatter in the DecimalNotationFormatter_Format

Here are the supported format options (with unlimited precision):

  • The general ("G") format specifier converts a number to a string that is formatted as the most compact of either fixed-point or scientific notation.
  • The scientific ("E") format specifier converts a number to a string of the form "-d.ddd…E+ddd" or "-d.ddd…e+ddd", where each "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative.
  • The fixed-point ("F") format specifier converts a number to a string of the form "-ddd.ddd…" where each "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative.
  • The standard numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol, "," indicates a group separator, and "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative *.
  • The percent ("P") format specifier multiplies the number by 100 and converts it to a string that represents a percentage.
  • The currency ("C") format specifier converts a number to a string that represents a currency amount.
  • The "R" format specifier attempts to ensure that a numeric value that is converted to a string is parsed back into the same numeric value: implemented via casting to double (maintaining interoperability at the cost of precision)
  • The significant digits after radix ("S") format specifier converts a number to a string that preserves the precision of the Fraction object with a variable number of digits after the radix point.
  • Custom format: these are only supported using the lossy ToDouble() conversion
  • Update the README.md to include another section about the DecimalNotationFormatter
  • Add benchmarks: the results appear to be within a magnitude difference of the equivalent ToDouble().ToString(...)

Description:
- Added a new DecimalFractionFormatter class for formatting Fraction objects into decimal string representations.
- Created tests for the DecimalFractionFormatter in the DecimalFractionFromatter_Format method to ensure correct functionality.
@danm-de
Copy link
Owner

danm-de commented Apr 12, 2024

That's pretty cool. I wish this article from Microsoft had existed years ago (or it hadn't been so well hidden). Then I would have used it as a guide for the ToString method.

I find this extension very useful. Unit tests are always valuable and necessary. However, this class would at least have to be mentioned and documented in the README.md. 😄

@lipchev lipchev changed the title Implement DecimalFractionFormatter and associated tests Implement DecimalFractionFormatter and associated tests (WIP) Apr 13, 2024
@lipchev
Copy link
Contributor Author

lipchev commented Apr 13, 2024

Just a couple of points to clear up:

  1. What do you think of the name, isn't it a bit ambiguous - I mean we are "using the decimal format to format a fraction" not necessarily "formatting a decimal fraction".. Any suggestions are welcome...
  2. What do you think about the "S" format- it is the "standard" format we've been using in UnitsNet. It isn't based on any official format as far as I'm aware.
  3. What do you think about the "R" format, although we can easily support the "a/b" formatting (like the DefaultFractionFormatter) I think we should keep it as "GXX" - but what value should we select for the precision ("XX")?

@danm-de
Copy link
Owner

danm-de commented Apr 13, 2024

  1. Ohha, the good old naming problem. I will try it:
  • DecimalNotationFormatter (fully qualified with namespace Fractions.Formatters.DecimalNotationFormatter)
  • DecimalFormatter (fully qualified with namespace Fractions.Formatters.DecimalFormatter)
    I know, pretty unimaginative and boring.
  1. S-Format: I'm not entirely sure - but I think my colleagues chose similar formatting for the WPF UI of the medical software. For dosage information, the essential digit after the decimal point must always be displayed. I rely on your expertise - if you think it's good and necessary, feel free to add it. The letter is not yet used in the standard defined by Microsoft. So I don't see any problems with that.

  2. As I understand the documentation, the round-trip format specifier is intended to guarantee that the string representation can be parsed back to exactly the same value. I therefore assume that this specifier is only used for purely technical applications (e.g. serialization). Although this formatter is written specifically for decimal - how do you want to ensure that 1/3 (0.33333...) can be parsed back to the exact Fraction value? I think that only works in fractional notation. The documentation also states that the precision specifier is ignored. Personally, I would choose numerator/denumerator (e.g. "1/3"). An interesting fun fact: the specifier for the DateTime data type is actually a small o instead of R. You actually always get the correct date - even with the time zone.
    image

As I wrote before - I find this formatter pretty cool and useful. My current implementation can only output decimal numbers in the representable range of the decimal data type. However, without instructions or mention in the documentation (README.md?) it will not be usable for most people (because they don't know it exists).

@danm-de
Copy link
Owner

danm-de commented Apr 13, 2024

If I haven't said it yet: thank you for your commitment and dedication. I really appreciate that.

- implemented the precise version of the percent format ("P")
- adjusted the variable names to match the project config
- updated the comments
- added the remaining tests
- updated the readme section
@lipchev lipchev changed the title Implement DecimalFractionFormatter and associated tests (WIP) Implement DecimalNotationFormatter and associated tests Apr 16, 2024
@danm-de danm-de merged commit 290fc30 into danm-de:master Apr 17, 2024
1 check passed
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

Successfully merging this pull request may close these issues.

2 participants