However, I can't do math operations on strings as I will get an error
var1 ='%.2f' % n1 ; var2 ='%.2f' % n2
result = var1 / var2
puts result
# Error as I cannot divide strings
How do I convert existing variables to more decimal places so that I can do
math with them?
However, I can't do math operations on strings as I will get an error
var1 ='%.2f' % n1 ; var2 ='%.2f' % n2
result = var1 / var2
puts result
# Error as I cannot divide strings
How do I convert existing variables to more decimal places so that I can do math with them?
If I understand you correctly, you want n1/n2 to give 1.25. In ruby a division
between two integers always return an integer value. The only way to have a
decimal number as a result is to convert one of the numbers to a float before
performing the division:
result = n1.to_f / n2
# => 1.25
I hope this helps
Stefano
···
On Friday, 15 September 2017 19:32:12 CEST Bill Bisco wrote:
In Ruby I can divide variables by other variables and get a numeric result
n1 = 500; n2 = 400
result = n1 / n2
puts "#{result}"
# Get back 1
I can convert existing variables to have more decimal places as strings
However, I can't do math operations on strings as I will get an error
var1 ='%.2f' % n1 ; var2 ='%.2f' % n2
result = var1 / var2
puts result
# Error as I cannot divide strings
How do I convert existing variables to more decimal places so that I can do
math with them?
You can replace the integers with floats when you assign them, as well.
Just add a decimal part to the value.
n1 = 500.0; n2 = 400.0
···
On Fri, Sep 15, 2017, at 06:32 PM, Bill Bisco wrote:
In Ruby I can divide variables by other variables and get a
numeric result>
n1 = 500; n2 = 400
result = n1 / n2
puts "#{result}"
# Get back 1
I can convert existing variables to have more decimal places as
>
var1 ='%.2f' % n1 ; var2 ='%.2f' % n2
# This gives n1 & n2 2 decimal points
However, I can't do math operations on strings as I will get an error> var1 ='%.2f' % n1 ; var2 ='%.2f' % n2
result = var1 / var2
puts result
# Error as I cannot divide strings
How do I convert existing variables to more decimal places so that I
can do math with them?>
Unsubscribe: <mailto:ruby-talk-request@ruby-
lang.org?subject=unsubscribe>> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
However, I can't do math operations on strings as I will get an error
var1 ='%.2f' % n1 ; var2 ='%.2f' % n2
result = var1 / var2
puts result
# Error as I cannot divide strings
How do I convert existing variables to more decimal places so that I can do math with them?
1. I can't get BigDecimal to register in my "interpreter"? (is that the
word?) in c9.io later. I'll play around with installing gems later
2. The suggestion to convert to floats is great and works
n1 = 500; n2 = 302
result = (n1/n2.to_f).round(3)
puts result
3. Robert's suggestion for converting to rational is great too!
n1 = 500; n2 = 401
result = "%.3f" % (n1.to_r/n2); puts result
4. It seems that Ruby's default # of decimals is 16 since the results for
this division
n1 = 500; n2 = 302
puts "#{n1/n2.to_f}"
is 1.6556291390728477
Therefore, if I need more precision past 16 decimal places, then I should
use the rational math code similar to #3. Otherwise, converting to float
similar to #2 is probably ok?
Thanks,
Bill
···
On Fri, Sep 15, 2017 at 2:04 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Fri, Sep 15, 2017 at 7:38 PM, Dan Fitzpatrick <dan@eparklabs.com> > wrote:
>
>> In Ruby I can divide variables by other variables and get a numeric
result
>>
>> n1 = 500; n2 = 400
>> result = n1 / n2
>> puts "#{result}"
>> # Get back 1
>>
>> I can convert existing variables to have more decimal places as strings
>>
>> var1 ='%.2f' % n1 ; var2 ='%.2f' % n2
>> # This gives n1 & n2 2 decimal points
>>
>> However, I can't do math operations on strings as I will get an error
>> var1 ='%.2f' % n1 ; var2 ='%.2f' % n2
>> result = var1 / var2
>> puts result
>> # Error as I cannot divide strings
>>
>> How do I convert existing variables to more decimal places so that I
can do math with them?
>
> Make one or both of them floats:
>
> n1 = 500; n2 = 400
> result = n1 / n2.to_f
Therefore, if I need more precision past 16 decimal places, then I should
use the rational math code similar to #3. Otherwise, converting to float
similar to #2 is probably ok?
The standard way to represent floating-point numbers stores them in a
fixed number of bits - if you want to learn the gory details you can
read here:
This, in simple words, means that the number of decimal places you
have is not constant (it varies with the exponent - how large the
number actually is). What is constant is the number of *significant*
digits.
If you want to have arbitrary precision, you use BigDecimal. The
disadvantages are that 1) your floating-point numbers will require
more memory, and a variable amount of it, and 2) that operations will
take longer and be more expensive, computation-wise.
Under Linux, Ruby uses the gmp library to manage BigDecimals:
If you digest the docs there, you will know more than I (I count the
occasions when I had to use arbitrary precision floating-point numbers
on the fingers of one hand, with some to spare).
Carlo
···
Subject: Re: Convert Existing numerical variables to have higher decimals?
Date: dom 17 set 17 01:49:53 -0400
--
* Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)