Giter VIP home page Giter VIP logo

Comments (5)

feist avatar feist commented on May 26, 2024 1

Also another (potentially serious) issue. If you pass "1.", to_f & to_i accept it, but to_d throws an exception.

2.4.1 :001 > require 'bigdecimal'
2.4.1 :002 > require 'bigdecimal/util'
2.4.1 :003 > "1.".to_i
=> 1
2.4.1 :004 > "1.".to_f
=> 1.0
2.4.1 :005 > "1.".to_d
ArgumentError: invalid value for BigDecimal(): "1."

from bigdecimal.

feist avatar feist commented on May 26, 2024

I'm seeing something even more worrying with bigdecimal-1.3.2

2.4.1 :001 > require 'bigdecimal'
=> true
2.4.1 :002 > require 'bigdecimal/util'
=> true
2.4.1 :006 > "1.".to_i
=> 1
2.4.1 :007 > "1.".to_f
=> 1.0
2.4.1 :008 > "1.".to_d
=> 0.0

from bigdecimal.

nzifnab avatar nzifnab commented on May 26, 2024

I'm also seeing this issue. Looks like it's somewhat related to this fix: #55

In ruby 2.3.3:

>> "30.".to_d
=> #<BigDecimal:55adc4a76918,'0.3E2',9(18)>

In ruby 2.4.1 (and updating bigdecimal to 1.3.2):

>> "30.".to_d
=> 0.0

from bigdecimal.

elfassy avatar elfassy commented on May 26, 2024

also

> "1.23other".to_d
=> 0.123e1
> "1.23degree".to_d
=> 0.0

from bigdecimal.

kakivis avatar kakivis commented on May 26, 2024

I guess the problem it that:
to_d tries to operate with "e"(also does the same with "d") on the string to multiplies or divide by 10 times the value that succeds the letter, but when encounters another letter, it somehow sets the value to 0.0 while to_f ignores the word.

that's why stuff like that happens:

> "1.23other".to_d
=> 0.123e1
> "1.23degree".to_d
=> 0.0
> "1.23kelvin".to_d
=> 0.123e1
> "1.23degree".to_f
=> 1.23

I'm having some struggle with a validation, because when it tries to validates an string with my regex it refuses stuff like "41avocado" but shows to the user the value "41.00" with error, cause after the typecast, the word is just ignored, but if the letter after the number was "e" or "d" the value is set to "0.00", wich is more user-friendly to be understood...

from bigdecimal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.