is there any way to convert a string like "7,1" into a number and keep the commas after the conversion?.
I've tried parseInt but that will ignore everything after the first value. Is there another way around it?
Convert a string into a number keeping the commas in js
762 views Asked by willd At
2
There are 2 answers
0
Shubham Jha
On
var str = '7,1';
// Split the above string variable into array
var numList = str.split(',');
// Type cast all these string values into number value
// Now we have an array of with each element in number format
numList = str.split(',').map((num) => Number(num));
console.log(numList);
// Now as per your requirement if you jion them with comma then it will again become string.
console.log(typeof (numList.join(',')));
// So technically it's not possible to have comma as a value in number variable.
// If you can tell a bit about your use case then we might be able to help you with some alternative.```
Related Questions in JAVASCRIPT
- Using Puppeteer to scrape a public API only when the data changes
- inline SVG text (js)
- An array of images and a for loop display the buttons. How to assign each button to open its own block by name?
- Storing the preferred font-size in localStorage
- Simple movie API request not showing up in the console log
- Authenticate Flask rest API
- Deploying sveltekit app with gunjs on vercel throws cannot find module './lib/text-encoding'
- How to request administrator rights?
- mp4 embedded videos within github pages website not loading
- Scrimba tutorial was working, suddenly stopped even trying the default
- In Datatables, start value resets to 0, when column sorting
- How do I link two models in mongoose?
- parameter values only being sent to certain columns in google sheet?
- Run main several times of wasm in browser
- Variable inside a Variable, not updating
Related Questions in STRING
- What does: "char *argv[]" mean?
- User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
- JSON Body is Not Passing Certain Strings
- Regex to match repeated substring in Google Sheets
- Find the sum of the numbers in the sequence
- Hello, how can I use a block parameter of withstyle parameter when we create a annotated string in jetpackpack compose
- How to convert an HTML string to an escaped one?
- Quintic Number Number Counting Hash Function
- From Buffer("string", "hex) to string JS
- Calling ToString with a nominated format returns Char rather than String
- How to update an already existing array by accessing it by a variable with the exact same name assigned to it
- Why does \b not interpreted as backslash in this regular expression
- Python: why aren’t strings being internalized if they are received from ints by using str()?
- If the element(s) in the first list equal element(s) of the second list, replace with element(s) of the third list
- About Suffix Trees features
Related Questions in NUMBERS
- Find the sum of the numbers in the sequence
- Partitions in co-lexicographic order (PARI/GP algorithm without recursion)
- Predict numbers from labeled images
- Increment number on each node with excluding one
- Ansible Increment Number on each node
- Find non numeric data for a column between duplicate key records
- Algorithm for rounding and clamping a number to specific ends
- Ahk gui does not display double digit numbers
- Using Javascript on Mac get selected row number from Numbers spreadsheet
- Peudorandom numbers in c++ not random enough
- Sensitive operations with big numbers in C++
- AppleScript: "System Events" from Terminal not working
- C++ converting binary string to decimal string
- How to convert HTML "span" + "input" elements to Number?
- Is there an easy way to get the max number from a String in PL/SQL
Related Questions in PARSEINT
- parseInt() function is adding 1 to my integer, and thus changing the number. What is going on?
- A small strange behavior of map with parseInt in JavaScript
- How to save and read variables to internal storage in android studio
- In Java, why does `Integer.parseInt("10000000000000000000000000000000"), 2)` throw a NumberFormatException?
- Integer.parseInt() throws NumberFormatException for some numbers read using BufferedReader from one file but not another
- Get just numbers from a list Content HTML
- Blank Cell with Celltype: String is throwing FormatException trying to parse as an int with NPOI
- Java string immutability and NumberFormatException For input string: ""
- Parse string to integer in arbitrary base/radix
- Converting hex strings to decimal format: Why am I getting different results in JavaScript and Python?
- ParseInt string by calling a function to return integer value
- parseInt returns NaN for normal-looking numeric strings. Why?
- java.lang.NumberFormatException: For input string: "488.15 EUR"
- Convert a string into a number keeping the commas in js
- How to convert string to integer when we have comma (,) in between word when used in java code
Related Questions in PARSEFLOAT
- Why doesn't parseFloat() fix NaN (Javascript)?
- Adobe JavaScript - Summing two values with a comma
- Why do I get 90.370+1.180+8.450 = 100.00000000000001 in Javascript?
- Javascript precision with more than 15 decimals
- FireFox or Safari + JavaScript Parses an Input Field Type Number Incorrectly
- When random number hits certain value, h1 or button should change color
- I am having an issue correctly appending decimal pointers in my small Basic Calculator App
- Convert a string into a number keeping the commas in js
- javacript library to convert string into float
- For float num with 0-2 decimals, if x+y=z in decimal and MIN_VALUE<=x,y,z<=MAX_VALUE, is Number.parseFloat((x+y).toFixed(2))==z always true?
- Does a hardcode float num a.bc... (0<=n<100 decimals,MIN_VALUE<=a.bc...<=MAX_VALUE) always equals to Number.parseFloat((a.bc...).toFixed(n))?
- JavaScript: turn a string into a number and then turn it back to string
- Convert Int to Float Without return String Javascript
- React native operation btw 2 floats
- How should I troubleshootiing 'strconv.ParseFloat: parsing "to": invalid syntax' in prometheus
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
You can't keep commas when you convert them to numbers, however you can use
.join()method on JS Arrays. Here is how you can do it.