Sorting arrays containing numbers with 3 decimals and keeping trailing zeroes (Ruby) -


i have array following numbers

numbers = [70.920, -38.797, 14.354, 99.323, 90.374, 7.581] 

i sort them numerically, problem ruby automatically rounds numbers trailing zeroes. keep format 3 decimal places , trailing zeroes ignored.

when declare numbers array, output.

=> [70.92, -38.797, 14.353, 99.323, 90.374, 7.581] 

i tried map array include 3 decimal places , convert them floats.

number.map { |n| "%.3f" % n }.map(&:to_f).sort 

once again, ruby trims trailing zeroes.

[-38.797, 7.581, 14.354, 70.92, 90.374, 99.323] 

i expecting input this

[-38.797, 7.581, 14.354, 70.920, 90.374, 99.323] 

i've used float documentation reference, can't figure out how implement this

http://ruby-doc.org/core-2.2.0/float.html

is possible in ruby? in advance.

are using input representation trailing zeros visualization purposes? can use array ruby use it, removing trailing zeroes. , when need present input, use like:

numbers.sort.map{|n|sprintf "%.3f", n} 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -