How to Make Single Digit Number a Two Digit Number in Ruby

How to make single digit number a two digit number in ruby?

For your need I think the best is still

Time.strftime("%m")

as mentioned but for general use case the method I use is

str = format('%02d', 4)
puts str

depending on the context I also use this one which does the same thing:

str = '%02d %s %04d' % [4, "a string", 56]
puts str

Here is the documentation with all the supported formats: http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-sprintf

Ruby on Rails: How do you add add zeros in front of a number if it's under 10?

Did you mean sprintf '%02d', n?

irb(main):003:0> sprintf '%02d', 1
=> "01"
irb(main):004:0> sprintf '%02d', 10
=> "10"

You might want to reference the format table for sprintf in the future, but for this particular example '%02d' means to print an integer (d) taking up at least 2 characters (2) and left-padding with zeros instead of spaces (0).

How to: multiple-digit integer, extract each integer into an array of single integers

n = 830124

n.to_s.split('').map(&:to_i)
#=> [8, 3, 0, 1, 2, 4]

or, without converting characters to integers:

n.to_s.size.times.with_object([]) { |_,a| n,i = n.divmod(10); a.unshift(i) }
#=> [8, 3, 0, 1, 2, 4]

How to display output with two digits of precision

You can use this:

puts "Your balance is #{'%.02f' % a.rem}"

But remember that this code will round your result if you have more than 2 decimal places. Ex.: 199.789 will become 199.79.

Ruby- Help converting 4 digit year input into 2 digit output

Well, you'll have to update your code sample a little bit, try this:

 puts "Enter year:"
year = gets.chomp.to_i # 4 digits year like 2022
res = year % 100 # 2 digits year like 22
puts "Welcome to '#{res}"

Why does this ruby sequence not work for double digit numbers?

Sort array of strings or array of integers

[n1, n2, n3, n4, n5] is an array of strings, and strings are compared with lexicographic order.

["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"].sort
#=> ["1", "10", "11", "12", "2", "3", "4", "5", "6", "7", "8", "9"]

["12", "11", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"].sort_by(&:to_i)
#=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]

So you need :

print "your numbers from smallest to largest are: #{a.sort_by(&:to_i)}"

or just convert your string array to an integer array :

a = [n1, n2, n3, n4, n5].map(&:to_i)

print "your numbers from smallest to largest are: #{a.sort}"

Refactoring

Here's a shorter way to write your script :

puts "Hello participant today we will be rearranging your numbers from smallest to largest, press enter to continue!!"
gets

a = %w(first second third fourth fifth).map do |ordinal|
puts "Please enter your #{ordinal} number"
gets.to_i
end

puts "Your numbers from smallest to largest are: #{a.sort}"
gets

puts "Thank you for participating, See you next time!!"

Algorithm Challenge number formatting problem

Here is the function invoice_number in python

def invoice_number(invoice):
s = ''.join(x for x in invoice if x <= '9' and x >= '0')
n = len(s)
if n <= 3:
return s

w = ''
i = 0
while i + 3 <= n:
for j in range(0, 3):
w += s[i + j]
i += 3
w += ('-')
m = n - i
if m == 0: return w[:-1]
if m == 1: return w[:m-3] + '-' + s[-2:]
return w + s[i:]

Testing

print(invoice_number('1234567'))
print(invoice_number('12345678'))
print(invoice_number('abc123456789'))
print(invoice_number('1234abc5678xyz9foobar'))

123-45-67
123-456-78
123-456-789
123-456-789

Ruby four-digit number, how to check it whether they are two the same numbers

You can do that as follows.

r = /(\d)\d*\1/
gets.match?(r)    # when gets #=> "1231\n"
#=> true
gets.match?(r) # when gets #=> "1232\n"
#=> true
gets.match?(r) # when gets #=> "1234\n"
#=> false

We can write the regular expression in free-spacing mode to make it self-documenting.

r = /
(\d) # match a digit and save to capture group 1
\d* # match zero or more digits
\1 # match the contents of capture group 1
/x # specify free-spacing regex definition mode

See String#match?.


If you must begin with the integer

four_digit_num = gets.to_i

you could write

arr = four_digit_num.digits
arr.uniq.size < arr.size

or convert it to a string and apply the first method above:

four_digit_num.to_s.match?(r)


Related Topics



Leave a reply



Submit