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

Create to fixed function #551

Closed

Conversation

Davidjustice28
Copy link

@Davidjustice28 Davidjustice28 commented Mar 24, 2024

Issue

Changes

  • Created a new to_fixed_string function in strings module
  • Added unit test covering multiple edge cases

Notes

Functionality works, but I feel like my solution is a lot of code. I was fighting circle dependency issues and had to use probably too many externals. I am looking forward to this review. First time contributing to open source and only week using gleam so bare with me.

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Though it would be good to reduce the amount of code here. I'd like to always return a string too rather than a result: we can treat precision less than 0 as 0.

I think we could simplify by using one of these approaches:

  1. float-to-string and then recursively iterate on the string, pattern matching until the . is found and then pattern matching the number of desired characters.
  2. float-to-string and then split on . and take the number of desired characters
  3. use division and modulo to get the number left and right of the . and then convert those ints to strings and join with a ..

No new externals should be needed.

Could you add a test for a precision of 0 and -1 too please?

Thank you.

/// to_string_fixed(0.3, 3)
/// // -> Ok("0.300")
///
/// ```gleam
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these markdown code blocks should be properly closed

/// ## Examples
///
/// ```gleam
/// to_string_fixed(2.3, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name in the examples here doesn't match the code.

@richard-viney
Copy link
Contributor

Is this approach able to handle exponential notation?

float.to_string doesn't always give a nnnn.mmmm style response, it can also return exponentials:

float.to_string(0.000000000144) == "1.44e-10"

The unit tests don't currently cover this case AFAICT.

@lpil
Copy link
Member

lpil commented May 20, 2024

Closing due to inactivity. Please feel free to reopen! Thank you

@lpil lpil closed this May 20, 2024
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.

None yet

4 participants