Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 10
- 1.7777777
- 9.1
- 10
- 1.78
- 9.1
- parseFloat("123.456").toFixed(2);
- var numb = 123.23454;
- numb = numb.toFixed(2);
- var numb = 1.5;
- numb = +numb.toFixed(2);
- // Note the plus sign that drops any "extra" zeroes at the end.
- // It changes the result (which is a string) into a number again (think "0 + foo"),
- // which means that it uses only as many digits as necessary.
- Math.round(1.005 * 1000)/1000 // Returns 1 instead of expected 1.01!
- parseFloat("1.555").toFixed(2); // Returns 1.55 instead of 1.56.
- parseFloat("1.5550").toFixed(2); // Returns 1.55 instead of 1.56.
- // However, it will return correct result if you round 1.5551.
- parseFloat("1.5551").toFixed(2); // Returns 1.56 as expected.
- 1.3555.toFixed(3) // Returns 1.355 instead of expected 1.356.
- // However, it will return correct result if you round 1.35551.
- 1.35551.toFixed(2); // Returns 1.36 as expected.
- function roundNumber(num, scale) {
- if(!("" + num).includes("e")) {
- return +(Math.round(num + "e+" + scale) + "e-" + scale);
- } else {
- var arr = ("" + num).split("e");
- var sig = ""
- if(+arr[1] + scale > 0) {
- sig = "+";
- }
- return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + scale)) + "e-" + scale);
- }
- }
- function roundToTwo(num) {
- return +(Math.round(num + "e+2") + "e-2");
- }
- roundToTwo(1.005)
- 1.01
- roundToTwo(10)
- 10
- roundToTwo(1.7777777)
- 1.78
- roundToTwo(9.1)
- 9.1
- roundToTwo(1234.5678)
- 1234.57
- Number.prototype.round = function(places) {
- return +(Math.round(this + "e+" + places) + "e-" + places);
- }
- var n = 1.7777;
- n.round(2); // 1.78
- it.only('should round floats to 2 places', function() {
- var cases = [
- { n: 10, e: 10, p:2 },
- { n: 1.7777, e: 1.78, p:2 },
- { n: 1.005, e: 1.01, p:2 },
- { n: 1.005, e: 1, p:0 },
- { n: 1.77777, e: 1.8, p:1 }
- ]
- cases.forEach(function(testCase) {
- var r = testCase.n.round(testCase.p);
- assert.equal(r, testCase.e, 'didn't get right number');
- });
- })
- var str = 10.234.toFixed(2); // => '10.23'
- var number = Number(str); // => 10.23
- Math.ceil(num * 100)/100;
- (function(){
- /**
- * Decimal adjustment of a number.
- *
- * @param {String} type The type of adjustment.
- * @param {Number} value The number.
- * @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
- * @returns {Number} The adjusted value.
- */
- function decimalAdjust(type, value, exp) {
- // If the exp is undefined or zero...
- if (typeof exp === 'undefined' || +exp === 0) {
- return Math[type](value);
- }
- value = +value;
- exp = +exp;
- // If the value is not a number or the exp is not an integer...
- if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
- return NaN;
- }
- // Shift
- value = value.toString().split('e');
- value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
- // Shift back
- value = value.toString().split('e');
- return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
- }
- // Decimal round
- if (!Math.round10) {
- Math.round10 = function(value, exp) {
- return decimalAdjust('round', value, exp);
- };
- }
- // Decimal floor
- if (!Math.floor10) {
- Math.floor10 = function(value, exp) {
- return decimalAdjust('floor', value, exp);
- };
- }
- // Decimal ceil
- if (!Math.ceil10) {
- Math.ceil10 = function(value, exp) {
- return decimalAdjust('ceil', value, exp);
- };
- }
- })();
- // Round
- Math.round10(55.55, -1); // 55.6
- Math.round10(55.549, -1); // 55.5
- Math.round10(55, 1); // 60
- Math.round10(54.9, 1); // 50
- Math.round10(-55.55, -1); // -55.5
- Math.round10(-55.551, -1); // -55.6
- Math.round10(-55, 1); // -50
- Math.round10(-55.1, 1); // -60
- Math.round10(1.005, -2); // 1.01 -- compare this with Math.round(1.005*100)/100 above
- // Floor
- Math.floor10(55.59, -1); // 55.5
- Math.floor10(59, 1); // 50
- Math.floor10(-55.51, -1); // -55.6
- Math.floor10(-51, 1); // -60
- // Ceil
- Math.ceil10(55.51, -1); // 55.6
- Math.ceil10(51, 1); // 60
- Math.ceil10(-55.59, -1); // -55.5
- Math.ceil10(-59, 1); // -50
- > 0.014999999999999999 === 0.0150000000000000001
- true
- > var m = 0.0150000000000000001;
- > console.log(String(m));
- 0.015
- > var m = 0.014999999999999999;
- > console.log(String(m));
- 0.015
- /**
- * Converts num to a decimal string (if it isn't one already) and then rounds it
- * to at most dp decimal places.
- *
- * For explanation of why you'd want to perform rounding operations on a String
- * rather than a Number, see http://stackoverflow.com/a/38676273/1709587
- *
- * @param {(number|string)} num
- * @param {number} dp
- * @return {string}
- */
- function roundStringNumberWithoutTrailingZeroes (num, dp) {
- if (arguments.length != 2) throw new Error("2 arguments required");
- num = String(num);
- if (num.indexOf('e+') != -1) {
- // Can't round numbers this large because their string representation
- // contains an exponent, like 9.99e+37
- throw new Error("num too large");
- }
- if (num.indexOf('.') == -1) {
- // Nothing to do
- return num;
- }
- var parts = num.split('.'),
- beforePoint = parts[0],
- afterPoint = parts[1],
- shouldRoundUp = afterPoint[dp] >= 5,
- finalNumber;
- afterPoint = afterPoint.slice(0, dp);
- if (!shouldRoundUp) {
- finalNumber = beforePoint + '.' + afterPoint;
- } else if (/^9+$/.test(afterPoint)) {
- // If we need to round up a number like 1.9999, increment the integer
- // before the decimal point and discard the fractional part.
- finalNumber = Number(beforePoint)+1;
- } else {
- // Starting from the last digit, increment digits until we find one
- // that is not 9, then stop
- var i = dp-1;
- while (true) {
- if (afterPoint[i] == '9') {
- afterPoint = afterPoint.substr(0, i) +
- '0' +
- afterPoint.substr(i+1);
- i--;
- } else {
- afterPoint = afterPoint.substr(0, i) +
- (Number(afterPoint[i]) + 1) +
- afterPoint.substr(i+1);
- break;
- }
- }
- finalNumber = beforePoint + '.' + afterPoint;
- }
- // Remove trailing zeroes from fractional part before returning
- return finalNumber.replace(/0+$/, '')
- }
- > roundStringNumberWithoutTrailingZeroes(1.6, 2)
- '1.6'
- > roundStringNumberWithoutTrailingZeroes(10000, 2)
- '10000'
- > roundStringNumberWithoutTrailingZeroes(0.015, 2)
- '0.02'
- > roundStringNumberWithoutTrailingZeroes('0.015000', 2)
- '0.02'
- > roundStringNumberWithoutTrailingZeroes(1, 1)
- '1'
- > roundStringNumberWithoutTrailingZeroes('0.015', 2)
- '0.02'
- > roundStringNumberWithoutTrailingZeroes(0.01499999999999999944488848768742172978818416595458984375, 2)
- '0.02'
- > roundStringNumberWithoutTrailingZeroes('0.01499999999999999944488848768742172978818416595458984375', 2)
- '0.01'
- /**
- * Takes a float and rounds it to at most dp decimal places. For example
- *
- * roundFloatNumberWithoutTrailingZeroes(1.2345, 3)
- *
- * returns 1.234
- *
- * Note that since this treats the value passed to it as a floating point
- * number, it will have counterintuitive results in some cases. For instance,
- *
- * roundFloatNumberWithoutTrailingZeroes(0.015, 2)
- *
- * gives 0.01 where 0.02 might be expected. For an explanation of why, see
- * http://stackoverflow.com/a/38676273/1709587. You may want to consider using the
- * roundStringNumberWithoutTrailingZeroes function there instead.
- *
- * @param {number} num
- * @param {number} dp
- * @return {number}
- */
- function roundFloatNumberWithoutTrailingZeroes (num, dp) {
- var numToFixedDp = Number(num).toFixed(dp);
- return Number(numToFixedDp);
- }
- Math.round(value * 100) / 100
- function roundToTwo(value) {
- return(Math.round(value * 100) / 100);
- }
- function myRound(value, places) {
- var multiplier = Math.pow(10, places);
- return (Math.round(value * multiplier) / multiplier);
- }
- +(10).toFixed(2); // = 10
- +(10.12345).toFixed(2); // = 10.12
- (10).toFixed(2); // = 10.00
- (10.12345).toFixed(2); // = 10.12
- function round(x, digits){
- return parseFloat(x.toFixed(digits))
- }
- round(1.222, 2) ;
- // 1.22
- round(1.222, 10) ;
- // 1.222
- Math.round( num * 100 + Number.EPSILON ) / 100
- number = 1.2345;
- number.toFixed(2) // "1.23"
- number = 1; // "1"
- number.toFixed(5).replace(/.?0*$/g,'');
- function round(number, precision) {
- var pair = (number + 'e').split('e')
- var value = Math.round(pair[0] + 'e' + (+pair[1] + precision))
- pair = (value + 'e').split('e')
- return +(pair[0] + 'e' + (+pair[1] - precision))
- }
- round(0.015, 2) // 0.02
- round(1.005, 2) // 1.01
- function round(value, exp) {
- if (typeof exp === 'undefined' || +exp === 0)
- return Math.round(value);
- value = +value;
- exp = +exp;
- if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
- return NaN;
- // Shift
- value = value.toString().split('e');
- value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));
- // Shift back
- value = value.toString().split('e');
- return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
- }
- round(10.8034, 2); // Returns 10.8
- round(1.275, 2); // Returns 1.28
- round(1.27499, 2); // Returns 1.27
- round(1.2345678e+2, 2); // Returns 123.46
- round(1234.5678, -2); // Returns 1200
- round("123.45"); // Returns 123
- var roundUpto = function(number, upto){
- return Number(number.toFixed(upto));
- }
- roundUpto(0.1464676, 2);
- var result = (Math.round(input*100)/100);
- Number.prototype.round = function(places){
- places = Math.pow(10, places);
- return Math.round(this * places)/places;
- }
- var yournum = 10.55555;
- yournum = yournum.round(2);
- _.round(number, precision)
- _.round(1.7777777, 2) = 1.78
- > d3.round(1.777777, 2)
- 1.78
- > d3.round(1.7, 2)
- 1.7
- > d3.round(1, 2)
- 1
- myNumber.toLocaleString('en', {maximumFractionDigits:2, useGrouping:false})
- Math.round(num * 100)/100;
- function roundToX(num, X) {
- return +(Math.round(num + "e+"+X) + "e-"+X);
- }
- //roundToX(66.66666666,2) => 66.67
- //roundToX(10,2) => 10
- //roundToX(10.904,2) => 10.9
- const round = (x, n) =>
- parseFloat(Math.round(x * Math.pow(10, n)) / Math.pow(10, n)).toFixed(n);
- round(44.7826456, 4) // yields 44.7826
- round(78.12, 4) // yields 78.1200
- Math.round(num * 1e2) / 1e2
- number = 16.6666666;
- console.log(parseFloat(number.toFixed(2)));
- "16.67"
- number = 16.6;
- console.log(parseFloat(number.toFixed(2)));
- "16.6"
- number = 16;
- console.log(parseFloat(number.toFixed(2)));
- "16"
Add Comment
Please, Sign In to add comment