SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,189
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    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. #3
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •