Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a number to 10 decimal places in Ruby on Rails?

I have latitude and longitude values in my database out to 10 decimal places:

+----+---------------+-----------------+
| id | lat           | lng             |
+----+---------------+-----------------+
| 55 | 34.4208305000 | -119.6981901000 |
| 56 | 30.2671530000 |  -97.7430608000 |

I need to query the db for a match, but my current variable is a float with only 6 decimal places:

self.lat => 30.267153

How can I convert my float to have the extra decimal places so I get a match?

myloc = Marker.where("lat = ?", self.lat)

I've seen Decimal and BigDecimal docs. Are those the best approach?

Ruby 1.8.7, Rails 3. Thanks for any advice.

like image 778
heyrolled Avatar asked Feb 11 '11 22:02

heyrolled


People also ask

How do I limit decimal places in Ruby?

The round() method can be used to round a number to a specified number of decimal places in Ruby. We can use it without a parameter ( round() ) or with a parameter ( round(n) ).

How do you set precision in Ruby?

Example: # Ensure we store z as a float by making one of the numbers a float. z = 1/3.0 # Format the float to a precision of three. format('%<num>0.3f', num: z) # => "0.333" format('%<num>0.5f', num: z) # => "0.33333" # Add some text to the formatted string format('I have $%<num>0.2f in my bank account.

How do you round a number in Ruby?

Ruby | Numeric round() function The round() is an inbuilt method in Ruby returns a number rounded to a number nearest to the given number with a precision of the given number of digits after the decimal point. In case the number of digits is not given, the default value is taken to be zero.

How to round down to the nth decimal number in Ruby?

If you want to round down to nth decimal number, multiple ( 10^n ) and round down and divide it by ( 10^n ). Ruby def dec_floor(num, n) 1.upto(n) do num *= 10 end num.floor 1.upto(n) do num /= 10 end end

How do you round a number in Ruby?

The round () is an inbuilt method in Ruby returns a number rounded to a number nearest to the given number with a precision of the given number of digits after the decimal point. In case the number of digits is not given, the default value is taken to be zero.

How to format decimal places of one specific number in R?

If we want to format the decimal places of one specific number (or a vector of numbers), we can use the format function in combination with the round function and the specification nsmall. Consider the following R syntax: format ( round ( x, 3), nsmall = 3) # Apply format function # "10.766".

How do you round down a number to three decimal places?

Round a number down by using the ROUNDDOWN function. It works just the same as ROUND, except that it always rounds a number down. For example, if you want to round down 3.14159 to three decimal places: =ROUNDDOWN (3.14159,3) which equals 3.141


1 Answers

You can use sprintf—or it's simpler brother String#%—to format your floating point number as a string with 10 decimals:

f = 30.267153
s = "%0.10f" % f
#=> "30.2671530000"

This seems fishy to me, though; what types of fields are your lat and lng columns? What RDBMS are you using? Do you really want to be comparing floating point values exactly? Do you really want to be storing them as strings?

like image 137
Phrogz Avatar answered Nov 02 '22 19:11

Phrogz