How do I always round a number down in Ruby?
Asked Answered
C

2

5

For example, if I want 987 to equal "900".

Creolacreole answered 20/7, 2017 at 23:47 Comment(2)
Welcome to SO. I'd suggest reading stackoverflow.com/help/how-to-ask. It'll help you write solid questions that will yield (hopefully) good answers. And do you want the number to round down and be a string?Liberal
Hello and thank you for the warm welcome! Actually, I would like it to be an number.Creolacreole
T
16
n = 987
m = 2

n.floor(-m)
  #=> 900

See Integer#floor: "When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros."

or

(n / 10**m) * 10**m
  #=> 900
Traject answered 21/7, 2017 at 0:2 Comment(1)
Note to myself: this isn't present in ancient ruby versions like 2.2.10 and gives ArgumentError: wrong number of arguments (1 for 0). How can I find out, in which version it was added?Gorgias
B
1

You can use logarithms to calculate the best multiple to divide by.

  def round_down(n)
    log = Math::log10(n)
    multip = 10 ** log.to_i
    return (n / multip).to_i * multip 
  end

  [4, 9, 19, 59, 101, 201, 1500, 102000].each { |x|
     rounded = round_down(x)
     puts "#{x} -> #{rounded}"
  }

Result:

4 -> 4
9 -> 9
19 -> 10
59 -> 50
101 -> 100
201 -> 200
1500 -> 1000
102000 -> 100000

This trick is very handy when you need to calculate even tick spacings for graphs.

Bratwurst answered 21/7, 2017 at 0:23 Comment(1)
Depending on how you have the number, one could also consider n.to_s(10), str.size()-1 or i.bit_length. When knowing the decimal digits, you can use n.floor(-digits-1 as in Carys answer, which I find more elegant. Another way to say (n / multip).to_i * multip would be n - (n % multip).Gorgias

© 2022 - 2024 — McMap. All rights reserved.