fix some small issues with scientific notation, add more tests

This commit is contained in:
Vadim Graboys 2015-07-31 16:22:14 -04:00
parent f402c8cd87
commit 9995dc92ee
2 changed files with 7 additions and 3 deletions

View file

@ -80,6 +80,7 @@ func New(value int64, exp int32) Decimal {
// d2, err := NewFromString(".0001")
//
func NewFromString(value string) (Decimal, error) {
originalInput := value
var intString string
var exp int64
@ -88,8 +89,7 @@ func NewFromString(value string) (Decimal, error) {
if eIndex != -1 {
expInt, err := strconv.ParseInt(value[eIndex+1:], 10, 32)
if err != nil {
rerr := err.(*strconv.NumError)
if rerr.Err == strconv.ErrRange {
if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange {
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value)
}
return Decimal{}, fmt.Errorf("can't convert %s to decimal: exponent is not numeric", value)
@ -119,7 +119,7 @@ func NewFromString(value string) (Decimal, error) {
if exp < math.MinInt32 || exp > math.MaxInt32 {
// NOTE(vadim): I doubt a string could realistically be this long
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value)
return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", originalInput)
}
return Decimal{

View file

@ -132,10 +132,14 @@ func TestNewFromStringErrs(t *testing.T) {
"1e9e",
"1ee9",
"1ee",
"1eE",
"1e-",
"1e-.",
"1e1.2",
"123.456e1.3",
"1e-1.2",
"123.456e-1.3",
"123.456Easdf",
"123.456e" + strconv.FormatInt(math.MinInt64, 10),
"123.456e" + strconv.FormatInt(math.MinInt32, 10),
}