Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3. Why do my decimal value rows are values such as #<BigDecimal:5003750,'0.1E2',9(18)>>?

Why do my decimal value rows are values such as #> instead of just a regular decimal number?

I have a cost column and I get weird values. Is that ok? If so why?

like image 480
leonel Avatar asked Feb 22 '23 17:02

leonel


2 Answers

That's just how Ruby prints out BigDecimal objects by default. Not sure why they chose such an ugly format, but hey - maybe some of the extra information can be useful.

Anyway, you should still be able to use them as you expect - it's just a bit of display weirdness. If you want to print a BigDecimal in a more normal format, call to_s on it first, or use puts, which calls to_s for you.

Hope that helps!

like image 126
Xavier Holt Avatar answered May 05 '23 14:05

Xavier Holt


Rails automatically casts the "row" decimal value into the Ruby object that it most resembles. In this case, a BigDecimal.

To print it out in a nice way, you can use "to_s" eg:

puts my_decimal.to_s
=> "3000000000000000000.0"

which should print it out nicer than the ugly class-named version you are seeing now.

like image 39
Taryn East Avatar answered May 05 '23 15:05

Taryn East