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 fmt.Formatter #326

Open
deitrix opened this issue Aug 29, 2023 · 3 comments · May be fixed by #366
Open

Implement fmt.Formatter #326

deitrix opened this issue Aug 29, 2023 · 3 comments · May be fixed by #366

Comments

@deitrix
Copy link

deitrix commented Aug 29, 2023

It would be nice if Decimal types implemented the fmt.Formatter interface to allow the use of standard fmt verbs/flags when formatting decimals to strings.

For example:

fmt.Printf("%.2f", decimal.RequireFromString("3.14"))

Would be equivalent to:

fmt.Printf("%s", decimal.RequireFromString("3.14").StringFixed(2))

There are a number of other verbs/flags that could be implemented which can be seen here

@mwoss
Copy link
Member

mwoss commented Dec 30, 2023

That would be an amazing addition to the library, thanks for the suggestion! :3 I won't be able to implement it before the 1.4 release, but I will add it to the backlog. Thanks!

u5surf pushed a commit to u5surf/decimal that referenced this issue Apr 23, 2024
* Fixes shopspring#326
* We would like to get started implement the fmt.Formatter.
* At first, we implement the indicators of '%s' and '%f'.
@u5surf
Copy link

u5surf commented Apr 23, 2024

@mwoss Hi, I've interested in this issue. Indeed, we would be good as if we used fmt.Formatter.
So I started this implementation, I have two questions. First, what should we do if it has the width of views into the format indicator? Because we cannot see the width of view above the example such as 5 of %5.2f , I don't see what should we consider the width of view?

fmt.Printf("%5.2f", decimal.RequireFromString("3.14")) 

Second, the concept of the Decimal's places, it can be placed with a negative number but what should we represent a negative number on the format indicator?

https://pkg.go.dev/fmt#hdr-Printing

@u5surf u5surf linked a pull request Apr 23, 2024 that will close this issue
@mwoss
Copy link
Member

mwoss commented Apr 28, 2024

Hi @u5surf! Thanks for working on this, I greatly appreciate that! :3
Answering your questions:

What should we consider the width of view?

I think in our case the format width parameter should be treated as padding. So in the above case you gave we should add spaces to the result on the left. There are other flags like 0 when we should fill those spaces with 0 instead of spaces.

The concept of the Decimal's places, it can be placed with a negative number but what should we represent a negative number on the format indicator?

In terms of formatting, I believe we should only support positive values for precision format parameters. I know the library itself supports negative places/precision, but for formatting, I don't think it's something necessary. If there will be a community need for that feature, we could add this later. :D

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 a pull request may close this issue.

3 participants