This question already has answers here:
A way to round Floats down
(11 answers)
Closed 6 years ago.
I have number num = 24.89808 and want to round it to 24.89
How can i do it?
i tried num.round(2) but it gives me 24.9 also number_to_currency=>24.90
If you are talking about rounding, 24.9 is by all means the correct result. Whether you are interested in ceiling it, here you go:
(24.89808 * 100).floor / 100.0
#⇒ "24.89"
First convert that into a decimal and then round it two two places,
Try this command,
num.to_d.round(2, :truncate).to_f
2.2.4 :040 > num = 24.89808
=> 24.89808
2.2.4 :041 > num.to_d.round(2,:truncate).to_f
=> 24.89
Related
This question already has answers here:
How to know if a number is odd or even in Swift?
(5 answers)
Closed 11 months ago.
im absolutely new to development, and trying to learn swift.
Right now i know how to make random number, and my next step is:
Im trying to understand how to check if my random number (127) could be divided by 2 without decimals ?
I have no idea how to do it.
There is a specific API isMultiple(of:) in Standard Library for this purpose
let random = Int.random(in: 0..<100)
let isEven = random.isMultiple(of: 2)
You can use operator % - remainder operator
example:
if randomNumber % 2 == 0 {
print("\(randomNumber) is even")
} else {
print("\(randomNumber) is odd")
}
This question already has answers here:
ruby floating point errors
(3 answers)
Closed 2 years ago.
prices = ["1.6", "0.15", "1.8"]
prices.sum { |price| price.to_f }
But this returns 3.5500000000000003, not 3.55.
Any idea?
Floating-point numbers cannot precisely represent all real numbers, and floating-point operations cannot precisely represent true arithmetic operations, this leads to many surprising situations.
I advise reading: https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
You may want to use BigDecimal to avoid such problems:
require 'bigdecimal'
prices = ["1.6", "0.15", "1.8"]
prices.sum { |price| BigDecimal(price) }
#=> 3.55
This question already has answers here:
How to convert a double to an int in Dart?
(11 answers)
How do you round a double in Dart to a given degree of precision AFTER the decimal point?
(28 answers)
Closed 3 years ago.
I want to round a double.
Double x = 5.56753;
x.toStringAsFixed(2);
When i put this, it gives 5.57000.
But i want to get 5.57. How do i get it?
there is num class contained function round():
Num
double numberToRound = 5.56753;
print(numberToRound.round());
//prints 6
If you want decimals
double n = num.parse(numberToRound.toStringAsFixed(2));
print(n);
//prints 5.57
check comment sujestion
For rounding doubles checkout: https://api.dartlang.org/stable/2.4.0/dart-core/double/round.html
Rounding won't work in your case because docs says:
Returns the integer closest to this.
So it will give 6 instead 5.57.
Your solution:
double x = 5.56753;
String roundedX = x.toStringAsFixed(2);
print(roundedX);
This question already has answers here:
to_d to always return 2 decimals places in ruby
(5 answers)
Closed 7 years ago.
i want to round off a number upto two decimal place in ruby such that
(0.02 * 270187).round(2) is 5403.74 which is correct
but
(0.02 * 278290).round(2) is 5565.8 which is not consistent with previous one
i want to make it look like 5565.80
Please tell me how can i do it in ruby
This will do the trick:
> sprintf("%.2f",(0.02 * 270187))
#=> "5403.74"
> sprintf("%.2f",(0.02 * 278290))
#=> "5565.80"
> sprintf("%.2f",(0.02 * 270187)).to_f > 100 # If you plan to Compare something with result
#=> true
OR
> '%.2f' % (0.02 * 270187)
#=> "5403.74"
> '%.2f' % (0.02 * 278290)
#=> "5565.80"
Demo
Note: The result is always a string, but since you're rounding I assume you're doing it for presentation purposes anyway. sprintf can format any number almost any way you like. If you are planning to compare anything with this result then convert this string to float by adding .to_f at the end. Like this
You could do something like
include ActionView::Helpers::NumberHelper
number_with_precision(value, :precision => 2) # value.to_f if you have string
or like this
'%.2f' % your_value
Hope it helps!
Further you can read from here
This question already has answers here:
Ruby: How to iterate over a range, but in set increments?
(4 answers)
Closed 8 years ago.
I would like to get a range of numbers but only by 5. Is there method for this? Perhaps something like: (0..100).by(5) (I feel like I've seen this done somewhere...)
I know I can do this: (0..100).select{|x| x if x % 5 == 0} Can you suggest alternatives?
Look at the method Numeric#step. I am sure it is what, you want.
0.step(25,5).to_a
# => [0, 5, 10, 15, 20, 25]
You want #step .
(0..100).step(5)....