question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should parse long number string as string instead of number

See original GitHub issue

Checklist

  • Have you asked your question on Stackoverflow or similar forum?
  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

Input

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <LongNumberString>420926189200190257681175017717</LongNumberString>
</Root>

Code

const { getTraversalObj, convertToJson } = require('fast-xml-parser');

const options = { attributeNamePrefix: '$', ignoreAttributes: false };

const x2j = xml => convertToJson(getTraversalObj(xml, options), options);

const xml = `<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <LongNumberString>420926189200190257681175017717</LongNumberString>
</Root>
`;

const json = x2j(xml);

console.log(json);

Output

{ Root: { LongNumberString: 4.209261892001902e+29 } }

expected data

{ Root: { LongNumberString: "420926189200190257681175017717" } }

Would you like to work on this issue?

  • Yes
  • No

Bookmark this repository for further updates.

Description

I can help to enhance the parser if you think this is correct. IMO, the scientific notation can not represent the value in the XML. The type should be string if the number string is too long (more than Number.MAX_SAFE_INTEGER).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
amitguptagwlcommented, Oct 2, 2019

@ShinyChang please use option parseTrueNumberOnly : true

3reactions
amitguptagwlcommented, Sep 14, 2019

Thanks for the suggestion. Will implement it in a future version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I parse a long number string to just a number?
The parseInt() function takes the first number of the string and removes decimal places, it returns an integer:.
Read more >
How to convert a string to a number - C# Programming Guide
You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long ,...
Read more >
Number() vs parseInt() - this vs that
Both Number() and parseInt() are often used to convert a string to number. Differences. Number() converts the type whereas parseInt parses the value...
Read more >
7 ways to convert a String to Number in JavaScript
1. Using parseInt() parseInt() parses a string and returns a whole number. Spaces are allo...
Read more >
How to Convert a String to a Number in JavaScript
One way to convert a string into a number would be to use the Number() ... Instead of multiplying by 1, you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found