Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8523991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:33:56+00:00 2026-06-11T07:33:56+00:00

Possible Duplicate: Arithmetic in ruby The status_update model takes 2 values in, does some

  • 0

Possible Duplicate:
Arithmetic in ruby

The status_update model takes 2 values in, does some simple calculations with them, and assigns ‘current_lbm’ and ‘current_bf_pct’ from there.

current_lbm and current_bf_pct will produce massive decimals if I don’t restrain them so I used the .round(2) method to limit the values to 2 decimal places.

Here is the method that does this.

after_initialize :default_values
.
.
def default_values      
 if self.current_bf_pct >= 0.5
   self.current_bf_pct /= 100
    if self.current_bf_pct <= 0.04
      self.current_fb_pct *= 100
    end 
 end
 self.current_fat_weight = self.current_weight * self.current_bf_pct
 self.current_lbm = self.current_weight - self.current_fat_weight
 self.current_fat_weight = self.current_fat_weight.round(2)
 self.current_lbm = self.current_lbm.round(2)
end   

Here are the results:

09/13/2012 186.0 4.5 177.63 8.37
09/13/2012 187.0 5.5 176.72 10.29
09/13/2012 188.0 6.5 175.78 12.22
09/13/2012 189.0 7.5 174.83 14.18
09/13/2012 190.0 8.5 173.85 16.15
09/13/2012 191.0 9.5 172.86 18.15
09/13/2012 192.0 10.5 171.84 20.16
09/13/2012 193.0 11.5 170.81 22.2
09/13/2012 194.0 12.5 169.75 24.25
09/13/2012 195.0 13.5 168.68 26.33
09/13/2012 196.0 14.499999999999998 167.58 28.42
09/13/2012 197.0 15.5 166.47 30.54
09/13/2012 198.0 16.5 165.33 32.67
09/13/2012 199.0 17.5 164.18 34.82
09/13/2012 200.0 18.5 163.0 37.0   

my question is… what the heck is up with 14.499999999999998??

These are the results from the view. The current_bf_pct row is the 3rd one from the dates. They aren’t decimals because I multiply them by 100 in the view.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:33:57+00:00Added an answer on June 11, 2026 at 7:33 am

    You can’t avoid this if you’re using displaying floats as-is (see Arithmetic in ruby). I suggest you convert your floats to strings for display.

    Example (your actual result may vary):

    >> 7.3-7.2
    => 0.09999999999999964
    

    Convert to string with 1 decimal place:

    >> '%.1f' % (7.3-7.2)
    => "0.1"
    

    The ‘%’ operator is shorthand for:

    sprintf '%.1f', (7.3-7.2)
    

    Read up on sprintf. It’s a bit of a learning curve but it’s very powerful.

    Another solution is to use BigDecimal (the 2nd argument is the precision – make sure you understand what this means):

    BigDecimal(7.3-7.2, 1).to_s
    

    If you use a large enough precision, you’ll see the problem again:

    >> BigDecimal(7.3-7.2, 16).to_s
    => "0.09999999999999964"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Performing simple arithmetic in MySQL statement or in PHP code Im wondering
Possible Duplicate: Pointer Arithmetic In C Code: int main() { int a[ ] ={0,1,2,3,4};
Possible Duplicate: Confused about C macro expansion and integer arithmetic A riddle (in C)
Possible Duplicate: C compiler bug (floating point arithmetic)? I've got two doubles which I
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: void * arithmetic Hi guys I have a small question regarding pointer
Possible Duplicate: working with incredibly large numbers in .NET I'm working on a simple
Possible Duplicate: What do these operators do? I'm working with some javascript for html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.