From: mail@... Date: 2017-10-10T12:53:51+00:00 Subject: [ruby-core:83198] [Ruby trunk Feature#13693] Allow String#to_i and / or Kernel::Integer to parse e-notation Issue #13693 has been updated by sos4nt (Stefan Sch����ler). > We also wondered whether you have an actual use case. I stumbled across this behavior when playing with a simple calculator which collects user input via: ```ruby number = Integer(gets) ``` > Exponential notation is usually used to express very large numbers or very small numbers that don't fit integer. I'd remove the _"that don't fit integer"_ part, but yes, it's a way to enter and display very large (or very small) numbers more concise. That's exactly was I was attempting: entering `1e30`, so I don't have to type `1000000000000000000000000000000`. > Consistency is important, but we don't add something just for 'consistency's sake. The documentation for [`Kernel#Integer`](http://ruby-doc.org/core-2.4.2/Kernel.html#method-i-Integer) explains that _"[...] strings should be strictly conformed to numeric representation"_. Shouldn't it in turn be able to parse for example `"1e30"`, given that `1e30` _is_ a valid [numeric representation](http://ruby-doc.org/core-2.4.2/doc/syntax/literals_rdoc.html#label-Numbers)? I think it's more a matter of completeness than consistency. It seems odd that `Integer()` happily parses `'0xff'`, `'0b100110'` and `'0740'` but cannot handle `'1e30'`. ---------------------------------------- Feature #13693: Allow String#to_i and / or Kernel::Integer to parse e-notation https://bugs.ruby-lang.org/issues/13693#change-67142 * Author: sos4nt (Stefan Sch����ler) * Status: Rejected * Priority: Normal * Assignee: * Target version: ---------------------------------------- `Kernel` can properly convert e-notation strings into numeric objects: ```ruby Float('1e+3') #=> 1000.0 Rational('1e+3') #=> (1000/1) Complex('1e+3') #=> (1000.0+0i) BigDecimal('1e+3') #=> 0.1e4 ``` Same for `String`: ```ruby '1e+3'.to_f #=> 1000.0 '1e+3'.to_r #=> (1000/1) '1e+3'.to_c #=> (1000.0+0i) '1e+3'.to_d #=> 0.1e4 ``` With one exception: ```ruby Integer('1e+3') #=> ArgumentError: invalid value for Integer(): "1e+3" '1e+3'.to_i #=> 1 ``` Ruby should be able to convert e-notation strings to integers, too. -- https://bugs.ruby-lang.org/ Unsubscribe: