# Thread: Problems dealing with large sums of money

1. ## Problems dealing with large sums of money

I'm trying to do some work manipulating some monetary values that number in the tens of trillions (with cent precision, i.e. 14 digits with two decimal places-16 digits total). It needs to be scalable even higher-at least to the hundreds of trillions and perhaps to a quadrillion. Anyway, I can get Ruby to take them in and parse them fine by bringing them in as strings and then using .to_d on them and storing them in a SQLite3 database with the column type set to "NUMERIC." I've checked in the database manager and can see they're being stored fine. I am having some issues reading the numbers back out, though-when I read them out of the database and run .to_s on them, it cuts off the last decimal place (i.e. if it's supposed to read 11552130832734.01, it reads 11552130832734.0). If it's under 10 trillion, it's fine, but at or over that the 16th digit is dropped.

To fix this, I've tried using .to_d (didn't work at all), .to_s(precision = 16), .to_s('D'), etc etc all to no avail. Does anyone have any ideas on how to get this to read out & display properly?

2. Hi gdracoulis, welcome to the forums,

AFAIK the general concensus is to use BigDecimal when working with money. Something about doing things as integer cents as opposed to decimal dollars to maintain precision.

Although there may still be problems with Ruby -> DB -> Ruby
http://dev.rubyonrails.org/ticket/5454

3. I thought that this might do it...

number = ((datatable[3].inner_html.delete(',').to_d)*100).to_i

to store the number and

(thisday.tpdo.to_f/100).to_s or (thisday.tpdo/100).to_s

to display it (after retrieval from the database)
where before I was using

datatable[3].inner_html.delete(',').to_d

and

thisday.tpdo.to_s

respectively. I can get the new code to, again, store the number properly. However, this causes problems when read back out before it's even put into the database. It cuts off BOTH decimals, I suppose because of its format as an integer (even though I tried converting to a float or a digit number by using .to_f and .to_d before doing the math on it and then converting it into a string, all to no avail). How can I pull this number back out without cutting off anything? I checked thisday.tpdo.class right after pulling it out of the database and it reads BigDecimal, and the program is handling it earlier in the routine as BigDecimal as well. I can clearly see in the database manager that it has maintained the precision, all the way up until the retrieval and conversion to string.