I have to send the amount of some price in cents to Stripe in order to make charge against a card. In my app, the total_price value is a decimal, i.e in dollars and cents. Obviously, I can convert this to cents by multiplying by 100:
total_price * 100
But the result is still a decimal, and Stripe gives me an 'Invalid amount' error. I know there can be issues with rounding floats. I want to know the safest way to cast my total_price to an integer in Rails. I have seen some reference to a money gem but is this necessary in this case?
Ruby has several methods available to floats, depending on what you need:
(cart.total_price * 100).to_i (discards all decimals)
(cart.total_price * 100).round # .round(numofdecimals)
(cart.total_price * 100).floor # 1.3 => 1
(cart.total_price * 100).ceil # 1.3 => 2
(cart.total_price * 100).to_r #to rationals, e.g. 2.5 => 5/2
I hope this helps.
There is actually a very easy way to do this, directly from a decimal without converting to a float first. Given:
decimal price = BigDecimal('100.00')
You can use BigDecimal's fix() and frac() methods to take the first part, multiply by 100 and add the fraction:
price_in_cents = ((decimal_price.fix * 100) + decimal_price.frac).to_i
Another seemingly simple possibility is to use string manipulation. You can convert the decimal to a string, strip the period, and convert to integer like:
price_in_cents = decimal_price.to_s.sub('.', '').to_i
But the gotcha here is that decimal_price.to_s == '100.0' not '100.00'. If there are non-zero digits in the fraction, then they would be preserved in the conversion, but otherwise one is lost. So if decimal_price == 111.11 then the above string manipulation would return the expected result.
Related
I have a list of float numbers, representing currency, and I need to turn them into integers, for precision.
The task is to turn float numbers into integers, likes:
0.95 => 95
1 => 100
1,465.01 => 146501
The problem is:
I don't have access to change the input csv files
The numbers came in a variety of ways (1.00, 1.0, 1, .95, 0.95, etc)
How can, safely, I turn these numbers into integers?
Some examples of my problem:
('16.81'.to_f * 100).to_i => 1680
('16.81'.to_f * 100_00).to_i / 100 => 1681
('342.28'.to_f * 100).to_i => 34228
('342.28'.to_f * 100_00).to_i / 100 => 34227
__ EDIT __
By the way, I'm using ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin19]
Floating point numbers can't necessarily represent all decimal numbers. This is explained in Is floating point math broken?. As such, when dealing with floats, you are always a bit uncertain and usually need to use rounding to get a desired number.
From your examples, ('16.81'.to_f * 100) results in 1680.9999999999998. Getting the integer value from that cuts off the fractional part, resulting in 1680. By using round instead, you can get the desired integer (which also solves the issue of partial cents). When relying on this, please note the details of how Ruby rounds exactly, specifically the optional half argument).
Instead of relying on Floats however, a better idea is to use BigDecimal numbers instead which allow arbitrary precision floating point numbers.
require 'bigdecimal'
(BigDecimal('16.81') * 100).round
# => 1681
(BigDecimal('.95') * 100).round
# => 95
number = '1,465.01'
# remove any "additional" characters
normalized = number.gsub(/[^-+0-9.]/, '')
(BigDecimal(normalized) * 100).round
# => 146501
In the last example, I have shown how you might cleanup your "human-readable" numbers for consistent parsing. Depending on your source data, you might need to perform additional changes (e.g. if you might have values such as '1.465,01' as is common in e.g. some European countries).
Use Bigdecimal for float numbers and append .to_i to convert it in integer
require 'bigdecimal'
(BigDecimal('16.81') * 100).to_i # 1681
(BigDecimal('342.28') * 100).to_i # 34228
For more details you can refer https://corainchicago.github.io/blog/why-does-ruby-add-numbers-wrong/
In javascript (or coffeescript), I have the following function:
bytesToMegabytes = (bytes) ->
return Math.round((b/1024/1024) * 100) / 100
I'm trying to recreate it in ruby. I have:
def bytes_to_megabytes (bytes)
(((bytes.to_i/1024/1024) * 100) / 100).round
end
But this rounds differently? For example, 1153597 becomes 1 in the ruby code.
I don't want to be a smart-ass, but no one seems to notice the calculation confusion here. 1 megabyte is simply 1000000 byte (google it). The 1024 is an outdated confusion about 10^2 byte which is 1024 kilobyte. Since 1998 this has become known as a kibibyte (wiki) and is now the norm.
That means you should just divide your byte by 1000000 and you're done. I've added some rounding for extra usefulness:
def bytes_to_megabytes (bytes)
(bytes.to_f / 1000000).round(2)
end
puts bytes_to_megabytes(1153597) # outputs 1.15
Try:
def bytes_to_megabytes (bytes)
bytes / (1024.0 * 1024.0)
end
bytes_to_megabytes(1153597)
#=> 1.1001558303833008
You can make CONSTANT variable like
MEGABYTES = 1024.0 * 1024.0
def bytes_to_megabytes (bytes)
bytes / MEGABYTES
end
bytes_to_megabytes(1153597)
#=> 1.1001558303833008
Now make you clear about to_i and round,
But this rounds differently? For example, 1153597 becomes 1 in the
ruby code.
to_i method take only decimal part it does not roundup the number for that you have to convert to float then roundup
> "148.68".to_i
#=> 148
> "148.68".to_f.round
#=> 149
As Stefen's comment : In Rails You can do simply like this:
> 1153597 / 1.0.megabyte
#=> 1.1001558303833008
> (1153597 / 1.0.megabyte).round
#=> 1
For more details of megabyte method Numeric#megabyte
1153597/1024
=> 1126
1153597/1024/1024
=> 1
This makes sense, since with integer division you get an integer result and 1153597 Bytes is roughly equal to 1MB. If you convert your input to a float first, it may be what you expect:
1153597.0/1024/1024
=> 1.1001558303833008
Therefore, change the code to use to_f instead of to_i, and remove round
def bytes_to_megabytes (bytes)
(((bytes.to_f/1024/1024) * 100) / 100)
end
With Ruby on Rails:
def bytes_to_megabytes(bytes)
(bytes.to_f/1.megabyte).round(2)
end
The equivalent of your JavaScript function would be
def bytes_to_megabytes (bytes)
(bytes.to_f / 1024 / 1024 * 100).round / 100.0
end
Ruby does integer division whereas JavaScript does floating-point division. Thus you have to make sure that at least one operand is a floating-point number.
I need to convert a latitude in ddmm.mmmmm (minutes in 4 decimal places) to ddmm.mmmmmm (minutes in 5 decimal places) format. Is there any good formula to convert this ?
I got the answer
We need to follow these steps for this conversion
1. Convert value in ddmm.mmmm format to dd.ddddddd by using the following formula
dd.ddddddd = dd + ( mm.mmmm / 60 )
convert back
ddmm.mmmmm = concat(dd, (.dddddd * 60))
Example:
To convert 3323.8733 from ddmm.mmmm format
convert to degrees (dd.dddd) format
33 + (23.8733 / 60 ) = 33.397888333333334
convert back to ddmm.mmmmm format
multiply decimal part by 60 i.e 0.397888333333334 * 60 => 23.87330000000004
append with degree
3323.87330000000004
As we need ddmm.mmmmm we can round of 5 decimal places i.e 3323.87330
Sans other information I would recommend following mkk's advice.
If you want to convert "ddmm.mmmmm" (4 decimal places) to "ddmm.mmmmm" (5 decimal places), you should probably just add a zero to the end.
Other methods may appear to give a more satisfactory result by placing a non-zero value in the fifth decimal place. But they cannot add more information than was present in the original number. There is, however, the potential to lose information through loss of significance in mathematical calculations.
I'm trying to recalculate percentages in an after_update callback of my model.
def update_percentages
if self.likes_changed? or self.dislikes_changed?
total = self.likes + self.dislikes
self.likes_percent = (self.likes / total) * 100
self.dislikes_percent = (self.dislikes / total) * 100
self.save
end
end
This doesn't work. The percentage always comes out as a 100 or 0, which completely wrecks everything.
Where am I slipping up? I guarantee that self.likes and self.dislikes are being incremented correctly.
The Problem
When you divide an integer by an integer (aka integer division), most programming languages, including Ruby, assume you want your result to be an Integer. This is mostly due to History, because with lower level representations of numbers, an integer is very different than a number with a decimal point, and division with integers is much faster. So your percentage, a number between 0 and 1, has its decimal truncated, and so becomes either 0 or 1. When multiplied by 100, becomes either 0 or 100.
A General Solution
If any of the numbers in the division are not integers, then integer division will not be performed. The alternative is a number with a decimal point. There are several types of numbers like this, but typically they are referred to as floating point numbers, and in Ruby, the most typical floating point number is of the class Float.
1.0.class.ancestors
# => [Float, Precision, Numeric, Comparable, Object, Kernel]
1.class.ancestors
# => [Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel]
In Rails' models, floats are represented with the Ruby Float class, and decimal with the Ruby BigDecimal class. The difference is that BigDecimals are much more accurate (ie can be used for money).
Typically, you can "typecaste" your number to a float, which means that you will not be doing integer division any more. Then, you can convert it back to an integer after your calculations if necessary.
x = 20 # => 20
y = 30 # => 30
y.to_f # => 30.0
x.class # => Fixnum
y.class # => Fixnum
y.to_f.class # => Float
20 / 30 # => 0
20 / 30.0 # => 0.666666666666667
x / y # => 0
x / y.to_f # => 0.666666666666667
(x / y.to_f).round # => 1
A Solution For You
In your case, assuming you are wanting integer results (ie 42 for 42%) I think the easiest way to do this would be to multiply by 100 before you divide. That pushes your decimal point as far out to the right as it will ever go, before the division, which means that your number is as accurate as it will ever get.
before_save :update_percentages
def update_percentages
total = likes + dislikes
self.likes_percent = 100 * likes / total
self.dislikes_percent = 100 * dislikes / total
end
Notes:
I removed implicit self you only need them on assignment to disambiguate from creating a local variable, and when you have a local variable to disambiguate that you wish to invoke the method rather than reference the variable
As suggested by egarcia, I moved it to a callback that happens before the save (I selected before_save because I don't know why you would need to calculate this percentage on an update but not a create, and I feel like it should happen after you validate that the numbers are correct -- ie within range, and integers or decimal or whatever)
Because it is done before saving, we remove the call to save in the code, that is already going to happen
Because we are not explicitly saving in the callback, we do not risk an infinite loop, and thus do not need to check if the numbers have been updated. We just calculate the percentages every time we save.
Because likes/dislikes is an integer value and integer/integer = integer.
so you can do one of two things, convert to Float or change your order of operations.
self.likes_percent = (self.likes.to_f/total.to_f) * 100
Or, to keep everything integers
self.likes_percent = (self.likes * 100)/total
I'm not sure that this is the only problem that you have, but after_update gets called after the object is saved.
Try changing the update_percentages before - on a before_update or a before_validate instead. Also, remove the self.save line - it will be called automatically later on if you use one of those callbacks.
I am storing a cost in my application. The cost is not formatted in the database. For example: 00.00 saves as 0, 1.00 saves as 1, and 40.50 saves as 40.5
I need to read these values from the database and convert them to strings for dollars and cents. For example: 0 --> cost_dollars = "00" & cost_cents = "00", 1 --> cost_dollars = "01" & cost_cents = "00", 40.5 --> cost_dollars = "40" & cost_cents = "50".
Is there an easy way to do this in ruby on rails? Or does someone have code that does this?
Thanks!
You can accomplish that with this little bit of Ruby code:
fmt = "%05.2f" % cost
cost_dollars, cost_cents = fmt.split '.'
If you're trying to format dollar values in a view, you should look at number_to_currency in ActionView::Helpers::NumberHelper.
>> bd = BigDecimal.new "5.75"
>> include ActionView::Helpers
>> number_to_currency(bd)
=> "$5.75"
As for breaking up the value into separate dollars and cents, my first question would be, "Why?" If you have a good reason, and you're dealing with decimals in your database, then you could do the following.
>> bd = BigDecimal.new "5.75"
>> "dollars:#{bd.truncate} cents:#{bd.modulo(1) * BigDecimal.new('100')}"
=> "dollars:5.0 cents:75.0"
number_to_currency is nice, but it can get expensive; you might want to roll your own if you need to call it a lot.
You should be aware that using a float to store currency can be problematic (and see) if you do a lot of calculations based on these values. One solution is to use integers for currency and count cents. This appears to be the approach used by the money plugin. Another solution is to use a decimal type in your migration, which should work out-of-the-box for modern versions of Rails (> 1.2):
add_column :items, :price, :decimal, :precision => 10, :scale => 2
(:scale is the number of places past the decimal, :precision is the total number of digits.) This will get you BigDecimal objects in Rails, which are a little harder to work with, but not too bad.
Both the integer and decimal approaches are a little slower than floating point. I'm using floats for currency in some places, because I know I won't need to do calculations on the values within Rails, only store and display them. But if you need accurate currency calculations, don't use floats.
Instead of storing as a decimal, store as an integral number of cents. So 1 dollar is stored as 100 in the database.
Alternatively, if you don't mind a bit of performance overhead, check for '.' in the database's value. If it exists, split on '.', and parse the pieces as integers.
sprintf is your friend here:
cost_dollars = sprintf('%02.f', cost)
cost_cents = sprintf('%.2f', cost)