Float - two digits after comma - how to? - ruby

I tried to execute a calculation in Ruby. The result I get is 1589.5833333333333. I would like to limitat the numbers of digits after the comma.
The result should always be limited to 2 digits as followed:
1589.58
Question #1 = how can I set the limitation?
Question #2 = how can I round up 1589.60 or down 1589.55
Many thanks for help. Language is ruby

Other option but keeping the object as Float:
n = 1589.5833333333333
m = n.truncate(2) #=> 1589.58
h = n.round(1) #=> 1589.6 # for the last zero you need to format the string
And a tricky:
k = (n*100).to_i.digits.tap{ |ary| ary.first > 5 ? ary[0] = 5 : ary[0] = 0 }.reverse.join('').to_i/100.0
#=> 1589.55

For question 1:
num = 1589.5833333333333
printf('%.2f', num)
=> 1589.58
For question 2 to round up to first digit:
num = 1589.5833333333333
printf('%.2f', num.round(1))
=> 1589.60
1589.55 is a bit of an arbitrary number, rounding down would usually be calculated as 1589.58. I don't know of any Ruby function that does that off-hand.

Related

Generate a sequence in Ruby

I want to take an integer from the user, and generate a sequence of five numbers starting with the given integer, and multiplying each previous number by four, using iteration. E.g., if I enter 2 then the list should be [2, 8, 32, 128, 512].
n = gets
i = 0
while i < 4
n = n * 4
p n
i = i + 1
end
I am not sure if you are looking to create an array of numbers to use later or just want to iterate. In the following you can replace puts memo with whatever you want, memo will contain the value you are looking for each pass.
s = 2
(s...(s+5)).reduce(s) do |memo, i|
puts memo
memo = memo * 4
end
Run:
(5 - 1).times.with_object([gets.to_i]){|i, a| a.push(a.last * 4)}
Input:
2
Return value:
[2, 8, 32, 128, 512]
There are a lot of ways to accomplish what you are describing. You already have some implementations in the comments and in the other answer by #Abhimanyu.
the trouble I'm having is to iterate the value for the sequence. I keep getting it as [2,2,2,2,2]
Your code example won't work because Kernel#gets doesn't return a number, but a String followed by a newline character (because it uses a default separator, from the global variable $/, which happens to be "\n"):
[1] pry(main)> gets
2
=> "2\n"
[2] pry(main)> gets.class
2
=> String
To fix your snippet you'll need to add .chomp.to_i to remove the newline and to convert it to an integer:
def generate_sequence
n = gets.chomp.to_i
i = 0
while i < 4
n = n * 4
p n
i = i + 1
end
end

How do I use multiple integers in an equation (Ruby)

I coded a conversion tool from binary to integer, but it had a limit on how large the number can be. So, I tried to code a formula for binary. I came up with an equation, so I tried to put it into code. Everything worked, except for applying the equation to each digit. This is the equation I came up with:
Let d represent the integer
Let z represent any (and every) digit
d = z[2^(z-1)]
This is what I've coded so far:
answer = gets.chomp
n = answer.reverse # reverses the answer
y1 = answer.size # the amount of digits in the answer
x1 = answer
z = (1..y1).each { |z| puts z } # every number between 1 and number of digits
w = (1..1).each.to_a * y1.to_i #in case I need to multiply the entire array
s = x1 # [z] - 1 # any given digit minus one
v = 2 ** s.to_i # exponent
u = z.zip(w).map{|x, y| x * y} # an array: [1, 2, 3]
print u
t = u.to_i # Tried converting to integer
puts x1[t]
But when I ran that, for example, with the number 1011, I got this error:
[1, 2, 3, 4]
undefined method `to_i' for [1, 2, 3, 4]:Array
Did you mean? to_s
to_a
to_h
(repl):16:in `<main>'
I feel like I have tried everything, but if you somehow find a way to apply the equation to every digit, or if you come up with a simpler equation, please tell me.
This return an array u = z.zip(w).map{|x, y| x * y} so you are triying to conver an array to integer. If you want, you can do something like this:
array = [1,0,1] #your binary in array form
s = array.join('') #transform it into string
s.to_i(2) #this return the integer and result (2) represents base
Check this link
And for better: array.join('').to_i(2)

Generating random number of length 6 with SecureRandom in Ruby

I tried SecureRandom.random_number(9**6) but it sometimes returns 5 and sometimes 6 numbers. I'd want it to be a length of 6 consistently. I would also prefer it in the format like SecureRandom.random_number(9**6) without using syntax like 6.times.map so that it's easier to be stubbed in my controller test.
You can do it with math:
(SecureRandom.random_number(9e5) + 1e5).to_i
Then verify:
100000.times.map do
(SecureRandom.random_number(9e5) + 1e5).to_i
end.map { |v| v.to_s.length }.uniq
# => [6]
This produces values in the range 100000..999999:
10000000.times.map do
(SecureRandom.random_number(9e5) + 1e5).to_i
end.minmax
# => [100000, 999999]
If you need this in a more concise format, just roll it into a method:
def six_digit_rand
(SecureRandom.random_number(9e5) + 1e5).to_i
end
To generate a random, 6-digit string:
# This generates a 6-digit string, where the
# minimum possible value is "000000", and the
# maximum possible value is "999999"
SecureRandom.random_number(10**6).to_s.rjust(6, '0')
Here's more detail of what's happening, shown by breaking the single line into multiple lines with explaining variables:
# Calculate the upper bound for the random number generator
# upper_bound = 1,000,000
upper_bound = 10**6
# n will be an integer with a minimum possible value of 0,
# and a maximum possible value of 999,999
n = SecureRandom.random_number(upper_bound)
# Convert the integer n to a string
# unpadded_str will be "0" if n == 0
# unpadded_str will be "999999" if n == 999999
unpadded_str = n.to_s
# Pad the string with leading zeroes if it is less than
# 6 digits long.
# "0" would be padded to "000000"
# "123" would be padded to "000123"
# "999999" would not be padded, and remains unchanged as "999999"
padded_str = unpadded_str.rjust(6, '0')
Docs to Ruby SecureRand, lot of cool tricks here.
Specific to this question I would say: (SecureRandom.random_number * 1000000).to_i
Docs: random_number(n=0)
If 0 is given or an argument is not given, ::random_number returns a float: 0.0 <= ::random_number < 1.0.
Then multiply by 6 decimal places (* 1000000) and truncate the decimals (.to_i)
If letters are okay, I prefer .hex:
SecureRandom.hex(3) #=> "e15b05"
Docs:
hex(n=nil)
::hex generates a random hexadecimal string.
The argument n specifies the length, in bytes, of the random number to
be generated. The length of the resulting hexadecimal string is twice
n.
If n is not specified or is nil, 16 is assumed. It may be larger in
future.
The result may contain 0-9 and a-f.
Other options:
SecureRandom.uuid #=> "3f780c86-6897-457e-9d0b-ef3963fbc0a8"
SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
For Rails apps creating a barcode or uid with an object you can do something like this in the object model file:
before_create :generate_barcode
def generate_barcode
begin
return if self.barcode.present?
self.barcode = SecureRandom.hex.upcase
end while self.class.exists?(barcode: barcode)
end
SecureRandom.random_number(n) gives a random value between 0 to n. You can achieve it using rand function.
2.3.1 :025 > rand(10**5..10**6-1)
=> 742840
rand(a..b) gives a random number between a and b. Here, you always get a 6 digit random number between 10^5 and 10^6-1.

Using for-loop to add numbers together

If I randomly put in two numbers (first number is smaller), how do I use a for-loop to add all the numbers between and itself?
ex:
first number: 3
second number: 5
the computer should give an answer of '12'.
How do I do that using a for-loop?
In Ruby we seldom use a for loop because it leaves litter behind. Instead, you can very simply do what you want using inject:
(3..5).inject(:+) # => 12
This is using some of the deeper Ruby magic (:+), which is a symbol for the + method and is passed into inject. How it works is a different question and is something you'll need to learn later.
Don't insist on doing something in a language using a particular construct you learned in another language. That will often force non-idiomatic code and will keep you from learning how to do it as other programmers in that language would do it. That creates maintenance issues and makes you less desirable in the workplace.
Simple for loop across the range you defined:
puts "Enter first number: "
first = gets.to_i
puts "Enter second number: "
second = gets.to_i
total = 0
for i in (first..second) do
total += i
end
puts total
Note that if you don't enter a valid number, it will converted to 0. Also this assumes the second number is larger than the first.
In Rails, or in plain-vanilla Ruby with ActiveSupport, you can do something even simpler than a for loop, or than what other people wrote.
(first_num..second_num).sum
This is shorthand for sum in Ruby:
sum = 0
(first_num..second_num).each { |num| sum += num }
first, second = [3,5]
for x in (0..0) do
p (first + second)*(second - first + 1) / 2
end
I know you said for loop, but why not use what Ruby gives you?
> a = 3
> b = 5
> a.upto(b).inject(0) {|m,o| m += o}
=> 12
If you insist on a for loop...
> m = 0
=> 0
> for i in 3..5
* m += i
* end
=> 3..5
> m
=> 12
Since Ruby 2.4 you directly call sum on an Enumerable.
For Example [1, 2, 3].sum #=> 6
In Ruby it's very rare to see a for loop. In this instance a more idiomatic method would be upto:
x = 3
y = 5
total = 0
x.upto(y) do |n|
total += n
end
puts total
# => 12
Another method would be to use reduce:
total = x.upto(y).reduce do |sum, n|
sum += n
end
...which can be shortened to this:
total = x.upto(y).reduce(&:+)

How do I take the sum of the digits of a square using Ruby?

I'm new to learning Ruby, and I'm trying to ask the user for an input, square that number, and then return the sum of the digits in the squared number. I'm confused when to use .to_s and .to_i, and I'd also like to accomplish this without using a shortcut.
Here's what I have so far (which doesn't work) :
def sqr_digit_sum(n)
square = (n ** 2).to_s
no_of_digits = square.size
sum = 0
i = 0
while i < no_of_digits
sum += square[i].to_i
i += 1
end
puts sum
end
I don't get why if I input 9 for a square of 81, square[0] returns 56 instead of 8. Can someone explain this?
It appears you’re using Ruby 1.8, where String#[] with a Fixnum argument (e.g. '8'[0]) returns the decimal ASCII character value. Which for '8' is 56. Since this is already a Fixnum, calling to_i on it has no effect. You should instead pass an index and length to []:
string = '81'
string[0, 1] #=> "8"
string[1, 1] #=> "1"
If you want all the characters in an array, though, you should use chars/each_char:
string = '81'
string.chars.to_a #=> ["8", "1"]
string.chars.to_a.map { |char| char.to_i } #=> [8, 1]
This makes implementing what you want fairly straightforward using the above and reduce:
n = 9
(n ** 2).to_s.chars.to_a.map { |char| char.to_i }.reduce(0, :+) #=> 9
Ultimately, though, you should upgrade to Ruby 2.0 (or at least 1.9.2+) as soon as possible as 1.8 is no longer supported and receives no security updates.
You could do this
def sqr_digit_sum(n)
square = (n ** 2)
puts square.to_s.split(//).inject(0) { |sum, number_as_string| number_as_string.to_i + sum }
end
Your code works just fine. Also, for the following code, the result is 8 as expected:
n = 9
square = (n ** 2).to_s[0]
puts square
Finally, may I suggest to refactor your code as:
def sqr_digit_sum(n)
sum = 0
(n ** 2).to_s.each_char { |c| sum += c.to_i }
puts sum
end
you don't really need a while loop there.

Resources