2

Is there some default configuration that can be altered to prevent scientific notation in the rails console without having to configure it on a per-column basis? (i.e. I'd like to just run some config line once somewhere and have it apply across all tables/columns for that app, if possible).

Example

Here is an example where scientific notation sort of gets in the way

Product.first

cost: 0.99999998999923e12,
currency: "USD",
charge: 0.55e1,
charge_currency: "AUD",
price: 0.5e2,
price_currency: "AUD"

I'd prefer to see 999999989999.23 instead of 0.99999998999923e12, 5.5 instead of 0.55e1, 50 instead of 0.5e2. I find regular numbers much more natural and easy to read.

What I know so far

There are a couple of ideas in the answers to Rails console shows value in scientific notation, but both are on a per column/attribute basis.

Note

I'd be happy to install a gem (I already use pry if that helps), and basically want a convenient way of avoiding scientific notation when displaying integers/floats/decimals but without having to write code specific to the model/columns/types.

stevec
  • 41,291
  • 27
  • 223
  • 311
  • Use the number helpers provided by [ActionView::Helpers::NumberHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html) or the [money gem](https://github.com/RubyMoney/money). What you're otherwise proposing would mean that you would have to monkeypatch `Numeric#to_s` and it's not a good idea - the core data class should not be concerned with how you want to present numbers. – max Dec 20 '22 at 11:21
  • As an alternative to the helpers you can use the [Decorator pattern](https://github.com/drapergem/draper). – max Dec 20 '22 at 11:23
  • Thanks @max, I appreciate the suggestions. I fear the ActionView helpers require implementation on a per column (or even per value) basis? Although it's from far outside the ruby ecosystem, [this](https://stackoverflow.com/a/25947542/5783745) is a great example of what I really hope I can find. Basically the R programming language also defaults to displaying in scientific notation, but with one tiny command - `options(scipen=999)` - it completely removes all scientific notation from the R console, which is hugely relieving. I hope I can find (or even make) something just as easy in rails. – stevec Dec 20 '22 at 11:28
  • The helpers are completly functional - you input a number and a format and it gives you back a formatted string. And yes you would have to implement it as an extra layer. – max Dec 20 '22 at 11:30
  • @max But wouldn't I have to use those formatters on a per value/column basis? What if the database is 1000 tables? Note the R example requires just one line of code to completely relieve the entire console session from any scientific notation. I don't think the same can be achieved with ActionView helpers, at least nowhere near as tersely? – stevec Dec 20 '22 at 11:32
  • The rest of your question has nothing to do with Rails really. Its fully possible to monkeypatch how Ruby displays numbers via `to_s` and `inspect` but doing so may unleash a maelstrom of bugs. Is it really worth the effort? – max Dec 20 '22 at 11:33
  • @max makes sense. I guess that rules out monkeypatching, as any high risk of bugs would be undesirable. – stevec Dec 20 '22 at 11:52

0 Answers0