Guest User

Untitled

a guest
Apr 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.60 KB | None | 0 0
  1. 10
  2. 1.7777777
  3. 9.1
  4.  
  5. 10
  6. 1.78
  7. 9.1
  8.  
  9. parseFloat("123.456").toFixed(2);
  10.  
  11. var numb = 123.23454;
  12. numb = numb.toFixed(2);
  13.  
  14. var numb = 1.5;
  15. numb = +numb.toFixed(2);
  16. // Note the plus sign that drops any "extra" zeroes at the end.
  17. // It changes the result (which is a string) into a number again (think "0 + foo"),
  18. // which means that it uses only as many digits as necessary.
  19.  
  20. Math.round(1.005 * 1000)/1000 // Returns 1 instead of expected 1.01!
  21.  
  22. parseFloat("1.555").toFixed(2); // Returns 1.55 instead of 1.56.
  23. parseFloat("1.5550").toFixed(2); // Returns 1.55 instead of 1.56.
  24. // However, it will return correct result if you round 1.5551.
  25. parseFloat("1.5551").toFixed(2); // Returns 1.56 as expected.
  26.  
  27. 1.3555.toFixed(3) // Returns 1.355 instead of expected 1.356.
  28. // However, it will return correct result if you round 1.35551.
  29. 1.35551.toFixed(2); // Returns 1.36 as expected.
  30.  
  31. function roundNumber(num, scale) {
  32. if(!("" + num).includes("e")) {
  33. return +(Math.round(num + "e+" + scale) + "e-" + scale);
  34. } else {
  35. var arr = ("" + num).split("e");
  36. var sig = ""
  37. if(+arr[1] + scale > 0) {
  38. sig = "+";
  39. }
  40. return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + scale)) + "e-" + scale);
  41. }
  42. }
  43.  
  44. function roundToTwo(num) {
  45. return +(Math.round(num + "e+2") + "e-2");
  46. }
  47.  
  48. roundToTwo(1.005)
  49. 1.01
  50. roundToTwo(10)
  51. 10
  52. roundToTwo(1.7777777)
  53. 1.78
  54. roundToTwo(9.1)
  55. 9.1
  56. roundToTwo(1234.5678)
  57. 1234.57
  58.  
  59. Number.prototype.round = function(places) {
  60. return +(Math.round(this + "e+" + places) + "e-" + places);
  61. }
  62.  
  63. var n = 1.7777;
  64. n.round(2); // 1.78
  65.  
  66. it.only('should round floats to 2 places', function() {
  67.  
  68. var cases = [
  69. { n: 10, e: 10, p:2 },
  70. { n: 1.7777, e: 1.78, p:2 },
  71. { n: 1.005, e: 1.01, p:2 },
  72. { n: 1.005, e: 1, p:0 },
  73. { n: 1.77777, e: 1.8, p:1 }
  74. ]
  75.  
  76. cases.forEach(function(testCase) {
  77. var r = testCase.n.round(testCase.p);
  78. assert.equal(r, testCase.e, 'didn't get right number');
  79. });
  80. })
  81.  
  82. var str = 10.234.toFixed(2); // => '10.23'
  83. var number = Number(str); // => 10.23
  84.  
  85. Math.ceil(num * 100)/100;
  86.  
  87. (function(){
  88.  
  89. /**
  90. * Decimal adjustment of a number.
  91. *
  92. * @param {String} type The type of adjustment.
  93. * @param {Number} value The number.
  94. * @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
  95. * @returns {Number} The adjusted value.
  96. */
  97. function decimalAdjust(type, value, exp) {
  98. // If the exp is undefined or zero...
  99. if (typeof exp === 'undefined' || +exp === 0) {
  100. return Math[type](value);
  101. }
  102. value = +value;
  103. exp = +exp;
  104. // If the value is not a number or the exp is not an integer...
  105. if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
  106. return NaN;
  107. }
  108. // Shift
  109. value = value.toString().split('e');
  110. value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
  111. // Shift back
  112. value = value.toString().split('e');
  113. return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
  114. }
  115.  
  116. // Decimal round
  117. if (!Math.round10) {
  118. Math.round10 = function(value, exp) {
  119. return decimalAdjust('round', value, exp);
  120. };
  121. }
  122. // Decimal floor
  123. if (!Math.floor10) {
  124. Math.floor10 = function(value, exp) {
  125. return decimalAdjust('floor', value, exp);
  126. };
  127. }
  128. // Decimal ceil
  129. if (!Math.ceil10) {
  130. Math.ceil10 = function(value, exp) {
  131. return decimalAdjust('ceil', value, exp);
  132. };
  133. }
  134. })();
  135.  
  136. // Round
  137. Math.round10(55.55, -1); // 55.6
  138. Math.round10(55.549, -1); // 55.5
  139. Math.round10(55, 1); // 60
  140. Math.round10(54.9, 1); // 50
  141. Math.round10(-55.55, -1); // -55.5
  142. Math.round10(-55.551, -1); // -55.6
  143. Math.round10(-55, 1); // -50
  144. Math.round10(-55.1, 1); // -60
  145. Math.round10(1.005, -2); // 1.01 -- compare this with Math.round(1.005*100)/100 above
  146. // Floor
  147. Math.floor10(55.59, -1); // 55.5
  148. Math.floor10(59, 1); // 50
  149. Math.floor10(-55.51, -1); // -55.6
  150. Math.floor10(-51, 1); // -60
  151. // Ceil
  152. Math.ceil10(55.51, -1); // 55.6
  153. Math.ceil10(51, 1); // 60
  154. Math.ceil10(-55.59, -1); // -55.5
  155. Math.ceil10(-59, 1); // -50
  156.  
  157. > 0.014999999999999999 === 0.0150000000000000001
  158. true
  159.  
  160. > var m = 0.0150000000000000001;
  161. > console.log(String(m));
  162. 0.015
  163. > var m = 0.014999999999999999;
  164. > console.log(String(m));
  165. 0.015
  166.  
  167. /**
  168. * Converts num to a decimal string (if it isn't one already) and then rounds it
  169. * to at most dp decimal places.
  170. *
  171. * For explanation of why you'd want to perform rounding operations on a String
  172. * rather than a Number, see http://stackoverflow.com/a/38676273/1709587
  173. *
  174. * @param {(number|string)} num
  175. * @param {number} dp
  176. * @return {string}
  177. */
  178. function roundStringNumberWithoutTrailingZeroes (num, dp) {
  179. if (arguments.length != 2) throw new Error("2 arguments required");
  180.  
  181. num = String(num);
  182. if (num.indexOf('e+') != -1) {
  183. // Can't round numbers this large because their string representation
  184. // contains an exponent, like 9.99e+37
  185. throw new Error("num too large");
  186. }
  187. if (num.indexOf('.') == -1) {
  188. // Nothing to do
  189. return num;
  190. }
  191.  
  192. var parts = num.split('.'),
  193. beforePoint = parts[0],
  194. afterPoint = parts[1],
  195. shouldRoundUp = afterPoint[dp] >= 5,
  196. finalNumber;
  197.  
  198. afterPoint = afterPoint.slice(0, dp);
  199. if (!shouldRoundUp) {
  200. finalNumber = beforePoint + '.' + afterPoint;
  201. } else if (/^9+$/.test(afterPoint)) {
  202. // If we need to round up a number like 1.9999, increment the integer
  203. // before the decimal point and discard the fractional part.
  204. finalNumber = Number(beforePoint)+1;
  205. } else {
  206. // Starting from the last digit, increment digits until we find one
  207. // that is not 9, then stop
  208. var i = dp-1;
  209. while (true) {
  210. if (afterPoint[i] == '9') {
  211. afterPoint = afterPoint.substr(0, i) +
  212. '0' +
  213. afterPoint.substr(i+1);
  214. i--;
  215. } else {
  216. afterPoint = afterPoint.substr(0, i) +
  217. (Number(afterPoint[i]) + 1) +
  218. afterPoint.substr(i+1);
  219. break;
  220. }
  221. }
  222.  
  223. finalNumber = beforePoint + '.' + afterPoint;
  224. }
  225.  
  226. // Remove trailing zeroes from fractional part before returning
  227. return finalNumber.replace(/0+$/, '')
  228. }
  229.  
  230. > roundStringNumberWithoutTrailingZeroes(1.6, 2)
  231. '1.6'
  232. > roundStringNumberWithoutTrailingZeroes(10000, 2)
  233. '10000'
  234. > roundStringNumberWithoutTrailingZeroes(0.015, 2)
  235. '0.02'
  236. > roundStringNumberWithoutTrailingZeroes('0.015000', 2)
  237. '0.02'
  238. > roundStringNumberWithoutTrailingZeroes(1, 1)
  239. '1'
  240. > roundStringNumberWithoutTrailingZeroes('0.015', 2)
  241. '0.02'
  242. > roundStringNumberWithoutTrailingZeroes(0.01499999999999999944488848768742172978818416595458984375, 2)
  243. '0.02'
  244. > roundStringNumberWithoutTrailingZeroes('0.01499999999999999944488848768742172978818416595458984375', 2)
  245. '0.01'
  246.  
  247. /**
  248. * Takes a float and rounds it to at most dp decimal places. For example
  249. *
  250. * roundFloatNumberWithoutTrailingZeroes(1.2345, 3)
  251. *
  252. * returns 1.234
  253. *
  254. * Note that since this treats the value passed to it as a floating point
  255. * number, it will have counterintuitive results in some cases. For instance,
  256. *
  257. * roundFloatNumberWithoutTrailingZeroes(0.015, 2)
  258. *
  259. * gives 0.01 where 0.02 might be expected. For an explanation of why, see
  260. * http://stackoverflow.com/a/38676273/1709587. You may want to consider using the
  261. * roundStringNumberWithoutTrailingZeroes function there instead.
  262. *
  263. * @param {number} num
  264. * @param {number} dp
  265. * @return {number}
  266. */
  267. function roundFloatNumberWithoutTrailingZeroes (num, dp) {
  268. var numToFixedDp = Number(num).toFixed(dp);
  269. return Number(numToFixedDp);
  270. }
  271.  
  272. Math.round(value * 100) / 100
  273.  
  274. function roundToTwo(value) {
  275. return(Math.round(value * 100) / 100);
  276. }
  277.  
  278. function myRound(value, places) {
  279. var multiplier = Math.pow(10, places);
  280.  
  281. return (Math.round(value * multiplier) / multiplier);
  282. }
  283.  
  284. +(10).toFixed(2); // = 10
  285. +(10.12345).toFixed(2); // = 10.12
  286.  
  287. (10).toFixed(2); // = 10.00
  288. (10.12345).toFixed(2); // = 10.12
  289.  
  290. function round(x, digits){
  291. return parseFloat(x.toFixed(digits))
  292. }
  293.  
  294. round(1.222, 2) ;
  295. // 1.22
  296. round(1.222, 10) ;
  297. // 1.222
  298.  
  299. Math.round( num * 100 + Number.EPSILON ) / 100
  300.  
  301. number = 1.2345;
  302. number.toFixed(2) // "1.23"
  303.  
  304. number = 1; // "1"
  305. number.toFixed(5).replace(/.?0*$/g,'');
  306.  
  307. function round(number, precision) {
  308. var pair = (number + 'e').split('e')
  309. var value = Math.round(pair[0] + 'e' + (+pair[1] + precision))
  310. pair = (value + 'e').split('e')
  311. return +(pair[0] + 'e' + (+pair[1] - precision))
  312. }
  313.  
  314. round(0.015, 2) // 0.02
  315. round(1.005, 2) // 1.01
  316.  
  317. function round(value, exp) {
  318. if (typeof exp === 'undefined' || +exp === 0)
  319. return Math.round(value);
  320.  
  321. value = +value;
  322. exp = +exp;
  323.  
  324. if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
  325. return NaN;
  326.  
  327. // Shift
  328. value = value.toString().split('e');
  329. value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));
  330.  
  331. // Shift back
  332. value = value.toString().split('e');
  333. return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
  334. }
  335.  
  336. round(10.8034, 2); // Returns 10.8
  337. round(1.275, 2); // Returns 1.28
  338. round(1.27499, 2); // Returns 1.27
  339. round(1.2345678e+2, 2); // Returns 123.46
  340.  
  341. round(1234.5678, -2); // Returns 1200
  342. round("123.45"); // Returns 123
  343.  
  344. var roundUpto = function(number, upto){
  345. return Number(number.toFixed(upto));
  346. }
  347. roundUpto(0.1464676, 2);
  348.  
  349. var result = (Math.round(input*100)/100);
  350.  
  351. Number.prototype.round = function(places){
  352. places = Math.pow(10, places);
  353. return Math.round(this * places)/places;
  354. }
  355.  
  356. var yournum = 10.55555;
  357. yournum = yournum.round(2);
  358.  
  359. _.round(number, precision)
  360.  
  361. _.round(1.7777777, 2) = 1.78
  362.  
  363. > d3.round(1.777777, 2)
  364. 1.78
  365. > d3.round(1.7, 2)
  366. 1.7
  367. > d3.round(1, 2)
  368. 1
  369.  
  370. myNumber.toLocaleString('en', {maximumFractionDigits:2, useGrouping:false})
  371.  
  372. Math.round(num * 100)/100;
  373.  
  374. function roundToX(num, X) {
  375. return +(Math.round(num + "e+"+X) + "e-"+X);
  376. }
  377. //roundToX(66.66666666,2) => 66.67
  378. //roundToX(10,2) => 10
  379. //roundToX(10.904,2) => 10.9
  380.  
  381. const round = (x, n) =>
  382. parseFloat(Math.round(x * Math.pow(10, n)) / Math.pow(10, n)).toFixed(n);
  383.  
  384. round(44.7826456, 4) // yields 44.7826
  385. round(78.12, 4) // yields 78.1200
  386.  
  387. Math.round(num * 1e2) / 1e2
  388.  
  389. number = 16.6666666;
  390. console.log(parseFloat(number.toFixed(2)));
  391. "16.67"
  392.  
  393. number = 16.6;
  394. console.log(parseFloat(number.toFixed(2)));
  395. "16.6"
  396.  
  397. number = 16;
  398. console.log(parseFloat(number.toFixed(2)));
  399. "16"
Add Comment
Please, Sign In to add comment