Rounding up to the nearest tenth
just replace p = round(cx_n);
with p = round(cx_n,1);
See MATLAB documentation: round
Y = round(X,N) rounds to N digits:
- N > 0: round to N digits to the right of the decimal point.
- N = 0: round to the nearest integer.
- N < 0: round to N digits to the left of the decimal point.
How to Round to the Nearest Tenth?
There's simple snippet at: http://lua-users.org/wiki/SimpleRound
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
It will misbehave when numDecimalPlaces is negative, but there's more examples on that page.
Round up to the nearest tenth?
This works in general:
ceil(number*10)/10
So in Ruby it should be like:
(number*10).ceil/10.0
round to the nearest tenth
Multiply by 10, then round then divide back by 10:
const round = (num) => Math.round(num * 10)/10
console.log(round(0.56))
console.log(round(2.78))
How would I round a number (eg: 2.12) to the nearest tenth (2.1) in JS
Method 1: The quick way is to use toFixed()
method like this:
var num = 2.12;
var round = num.toFixed(1); // will out put 2.1 of type String
One thing to note here is that it would round 2.12
to 2.1
and 2.15
to 2.2
Method 2: On the other hand you can use Math.round
with this trick:
var num = 2.15;
Math.round(num * 10) / 10; // would out put 2.2
It would round to the upper bound.
So, choose whichever you like.
Also if you use a modern version of JS ie. ES then using const
and let
instead for variable declaration might be a better approach.
NOTE: remember that .toFixed() returns a string. If you want a number, use the Math.round() approach. Thanks for the reminder @pandubear
Rounding floats to nearest 10th
Here's how to round a number to the nearest tenth:
>>> round(21.3331, 1)
21.3
Here's how to print out a floating point number with one decimal point:
>>> print "%.1f" % (21.3331,)
21.3
Note that if you do it using %d
it will get printed as rounded to the nearest integer:
>>> print "%d" % (21.3331,)
21
See the String Formatting Operations docs for more on how the %
formatting works.
Value should be rounded to nearest tenth or the nearest quarter
You can use a switch
statement.
Main func:
func differentRounding(value:Double) -> Double {
var decimalValue = value % 1
let intValue = value - decimalValue
switch decimalValue {
case 0.225..<0.265 :
decimalValue = 0.25
case 0.725..<0.765 :
decimalValue = 0.75
default :
decimalValue = ((decimalValue * 10).roundDownTillFive()) / 10
}
return decimalValue + intValue
}
differentRounding(1.2434534545) // 1.25
differentRounding(3.225345346) // 2.25
differentRounding(0.2653453645) // 0.3
differentRounding(0.05) // 0
differentRounding(0.04456456) // 0
Rounding down till 5
extension:
extension Double {
func roundDownTillFive() -> Double {
var decimalValue = self % 1
let intValue = self - decimalValue
switch decimalValue {
case 0.0...0.5 :
decimalValue = 0.0
case 0.5...1.0 :
decimalValue = 1.0
default :
break
}
return decimalValue + intValue
}
}
Double(0.5).roundDownTillFive() // 0
Double(0.50000001).roundDownTillFive() // 1
Round to the nearest tenth?
round($num, 1);
should round $num to the nearest tenth (the second argument specifies the precision, or the number of digits after the decimal it should round to)
how to round the decimal number to the nearest tenth in javascript
Do you actually want a number like 2758.59375 to round down to 2758.5? I would expect that number to be rounded to 2758.6, personally. Depending if you want it to round down, round up, or round to nearest, the code is slightly different.
The simplest way is to say:
> var num = 2758.59375
> num.toFixed(1)
2758.6
Related Topics
Best Practices For Reusing Code Between Controllers in Ruby on Rails
Unresolved Specs During Gem::Specification.Reset:
How to Switch to Ruby 1.9.3 Installed Using Homebrew
Why Can't I Install Rails on Lion Using Rvm
Installing Jekyll Without Root
Why Does Adding "Sleep 1" in an After Hook Cause This Rspec/Capybara Test to Pass
How to Use Activerecord in a Ruby Script Outside Rails
How to Wrap Link_To Around Some HTML Ruby Code
How to Use Bundler Behind a Proxy
Ruby Method Array#≪≪ Not Updating the Array in Hash
Need to Split Arrays to Sub Arrays of Specified Size in Ruby
How to Capture Stdout to a String
How to Use Global Variables or Constant Values in Ruby
Find Unused Code in a Rails App
How to 'Bundle Install' When Your Gemfile Requires an Older Version of Bundler