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
please add get original value funcion #237
Comments
Hi @piyongcai! That sounds like a good idea, I will add your request to my backlog. d := NewFromFloat(5.45)
d.Coefficient().Int64() |
// Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent()
func (d Decimal) Coefficient() *big.Int {
d.ensureInitialized()
// we copy the coefficient so that mutating the result does not mutate the
// Decimal.
return big.NewInt(0).Set(d.value)
} in Coefficient function, call big.NewInt(0).Set(d.value), this isn't a good idea. |
@piyongcai What do you mean by "this isn't a good idea"? Could you elaborate on that? |
Had the same problem and drafted a PR for this @ #241. |
@clstb Maybe I'm still asleep, but what is the difference between your proposed implementation and an example I've showed earlier |
You were not asleep but I was apparently. I plugged your workaround into my test case and it errored. I thought this Edit: After retrying it behaves how it should. Should have looked more carefully in the beginning 👍 |
So I guess the most suitable option here would be to extend API by adding the |
// Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent()
func (d Decimal) Coefficient() *big.Int {
d.ensureInitialized()
// we copy the coefficient so that mutating the result does not mutate the
// Decimal.
return big.NewInt(0).Set(d.value)
} Because this method has a very low performance。 // Int64Value returns the value.
func (d Decimal) Int64Value() int64 {
return d.value.Int64()
} In the usage scenario of protobuf, it is generally used for amount calculation, and the maximum decimal number is set to 4 bits.The message is defined as follows. // Example: 12345.6789 -> { units = 12345, exp = 4} = 1.2345
message Decimal {
// Whole units part of the amount
int64 val = 1;
// Nano units of the amount (10^-9)
// Must be same sign as units
int32 exp = 2;
} I can convert between the two in the simplest way |
Yup, so let's expose new public method called |
thanks |
#244 has been merged to main branch. |
3ks,it's works |
in current decimal,IntPart Function to get decimal IntPart value
BUT, IN SOM TIME, NEED TO GET ORIGINAL VALUE
eg: PROTOBUF , you need define Decimal message, and you need convert between proto.Decimal and decimal.Decimal
so, it's really neet to to get original value function,
To get this purpose, I copied decimal code, and Add Int64Value function
PLEASE ADD
PLEASE ADD
PLEASE ADD
the follw code is my proto Decimal define:
convert between decimal.Decimal and proto.Decimal
1、decimal.Decimal -> proto.Decimal
2、proto.Decimal -> decimal.Decimal
The text was updated successfully, but these errors were encountered: