Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

510
Views
How to reverse numeric value having scientific notation in nodeJS?

I want to understand best way to reverse an integer (both positive and negative) in NodeJS 12. Can we do this without converting number to string? It should also support scientific notation numbers like 1e+10 which is 10000000000.

Input/Expected Output
 
500 = 5
-94 = -49
1234 = 4321
-1 = -1
1e+10 = 1
123.45e+10 = 54321
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I hope this one line function solves your use-case

// The Math.sign() function returns either a positive or negative +/- 1, 
// indicating the sign of a number passed into the argument.   

function reverseInt(n) {
  return parseInt(n.toString().split('').reverse().join('')) * Math.sign(n)
}

console.log(reverseInt(500));
console.log(reverseInt(-94));
console.log(reverseInt(1234));

about 4 years ago · Juan Pablo Isaza Report

0

This is the answer I could come up with but I feel there might be better way. I didn't like the way I had to convert the integer to string, reverse it and again convert it back to integer.

parseInt(Math.sign(num) * parseInt(Math.abs(num).toString().split("").reverse().join("")))
about 4 years ago · Juan Pablo Isaza Report

0

function reverseNum(num) {
    return (
    parseFloat(
      num
        .toString()
        .split('')
        .reverse()
        .join('')
    ) * Math.sign(num)
  )
}

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!