Int64 returns the int64 representation of x. If x cannot be represented in an int64, an error is returned.
()
| 255 | // Int64 returns the int64 representation of x. If x cannot be represented in an |
| 256 | // int64, an error is returned. |
| 257 | func (d *Decimal) Int64() (int64, error) { |
| 258 | if d.Form != Finite { |
| 259 | return 0, fmt.Errorf("%s is not finite", d.String()) |
| 260 | } |
| 261 | var integ, frac Decimal |
| 262 | d.Modf(&integ, &frac) |
| 263 | if !frac.IsZero() { |
| 264 | return 0, fmt.Errorf("%s: has fractional part", d.String()) |
| 265 | } |
| 266 | var ed ErrDecimal |
| 267 | if integ.Cmp(decimalMaxInt64) > 0 { |
| 268 | return 0, fmt.Errorf("%s: greater than max int64", d.String()) |
| 269 | } |
| 270 | if integ.Cmp(decimalMinInt64) < 0 { |
| 271 | return 0, fmt.Errorf("%s: less than min int64", d.String()) |
| 272 | } |
| 273 | if err := ed.Err(); err != nil { |
| 274 | return 0, err |
| 275 | } |
| 276 | v := integ.Coeff.Int64() |
| 277 | for i := int32(0); i < integ.Exponent; i++ { |
| 278 | v *= 10 |
| 279 | } |
| 280 | if d.Negative { |
| 281 | v = -v |
| 282 | } |
| 283 | return v, nil |
| 284 | } |
| 285 | |
| 286 | // Float64 returns the float64 representation of x. This conversion may lose |
| 287 | // data (see strconv.ParseFloat for caveats). |