Menu

gitpiper

reverseNumber javascript Code Snippet in 2022

mathstringbeginner

Last Updated: 25 September 2022

Reverses a number.

  • Use Object.prototype.toString() to convert n to a string.
  • Use String.prototype.split(''), Array.prototype.reverse() and String.prototype.join('') to get the reversed value of n as a string.
  • Use parseFloat() to convert the string to a number and Math.sign() to preserve its sign.
const reverseNumber = n => parseFloat(`${n}`.split('').reverse().join('')) * Math.sign(n);
reverseNumber(981); // 189 reverseNumber(-500); // -5 reverseNumber(73.6); // 6.37 reverseNumber(-5.23); // -32.5

javascript snippet similar to reverseNumber For You in September 2022

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2022 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️