user1563849
user1563849

Reputation: 223

Dropping decimal point

I need to compare a data set in which one batch of data came with currency with decimals such as 246.54 the new data removes the decimals, does not round up, and has just 246... so I need to remove the decimals from the first batch so I can compare. How would I do this without rounding up?

Upvotes: 15

Views: 15333

Answers (3)

Keir Finlow-Bates
Keir Finlow-Bates

Reputation: 1063

if mynumber >= 0 then 
    mynumber = mynumber.floor
else
    mynumber = 0 - mynumber
    mynumber = mynumber.floor
    mynumber = 0 - mynumber
end

Upvotes: 1

Qsario
Qsario

Reputation: 1026

Use the floor function. It gives the first integer smaller than or equal to whatever number you feed it.

1.9.3-p194 :003 > i = 246.54
  => 246.54 
1.9.3-p194 :004 > i.floor
  => 246 

Upvotes: 22

user1706950
user1706950

Reputation: 42

You can do a rounding in ruby:

246.54 .to_nearest_i

Upvotes: -1

Related Questions