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

func (d Decimal) MustFloat64() #205

Merged
merged 2 commits into from Mar 29, 2021
Merged

Conversation

felipeneuwald
Copy link
Contributor

@felipeneuwald felipeneuwald commented Jan 17, 2021

Implementes func (d Decimal) MustFloat64().
Resolves #204.

@mwoss
Copy link
Member

mwoss commented Jan 18, 2021

Thanks for your contribution @felipeneuwald!
I was thinking for a moment about your PR for a bit and I'm not convinced whether the MustFloat method should be a part of the decimal library. I'm not sure this wrapper Method would be valuable for this library. Maybe it's just me, not sure :<

Note: Unit tests are missing.

What do you think? @njason

@felipeneuwald
Copy link
Contributor Author

felipeneuwald commented Jan 18, 2021

Hey guys,

There are cases that I don't mind if the returned float64 value is exact or not, and I want to assign the float64 value directly into a struct using a single line of code. See:

package main

import (
	"fmt"

	"github.com/shopspring/decimal"
)

type someType struct {
	a float64
	b float64
}

func main() {
	data1 := someType{
		a: float64(1),
		b: decimalToFloat64(decimal.NewFromInt(2)),
	}
	fmt.Printf("%+v\n", data1)

	myDecimal := decimal.NewFromInt(4)
	myFloat, _ := myDecimal.Float64()
	data2 := someType{
		a: float64(3),
		b: myFloat,
	}
	fmt.Printf("%+v\n", data2)

}

// decimalToFloat64 receives a d decimal.Decimal and returns a float64
func decimalToFloat64(d decimal.Decimal) float64 {
	f, _ := d.Float64()
	return f
}

In that case, the only way to use a single line of code to assign the value to data1.b is by using the function decimalToFloat64. A little bit of extra code is needed for data2.b.

There are similar cases in the standard library. See https://golang.org/pkg/html/template/#Must.

Regarding the unit test, I don't see any case that hasn't been covered in TestFloat64() - but if you guys think this is mandatory to move forward, just let me know, and I'll write the test.

Cheers, Felipe.

@mwoss
Copy link
Member

mwoss commented Jan 20, 2021

Thanks for showing your point of view on that Felipe!
Let's wait for Jason's opinion, as I want to hear what he thinks about it. :D

@twocs
Copy link

twocs commented Feb 7, 2021

In general, when I see a function named like MustFloat64, precedence is that it must panic if the underlying function would fail.
e.g. "panics if the error is non-nil" https://golang.org/src/text/template/helper.go

As the result of this PR would be a function that ignores any errors. I'd recommend a name such as:
InexactFloat64

@felipeneuwald
Copy link
Contributor Author

@twocs nice catch. Please check now. Cheers.

@mwoss
Copy link
Member

mwoss commented Mar 29, 2021

Looks good, merging :3

@mwoss mwoss merged commit 5016615 into shopspring:master Mar 29, 2021
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.

func (d Decimal) MustFloat64()
3 participants