arrays - Dividing fixnum to few parts in ruby -


i have prices (for example 1000000 , 1000). not matter kind of price (this can 345000 or 532). need divide price few parts (["1", "000", "000"] or ["345", "000"]).

how can this. ideas?

since example includes 000 can string (000 fixnum rendered 0) can split numbers in group of 3 digits doing:

num = 10000 # num res = num.to_s.reverse.scan(/.{1,3}/m).map(&:reverse).reverse  # res = ["10", "000"] 

otherwise if want divide last 0s , rest of number, can do:

num = 10000 # num res = [num.to_s.sub(/0+$/, '') ,num.to_s.scan(/0+$/)[0]]  # res = ["1", "0000"] 

Comments