last day (14 days later) » 

09:17
-3
Q: How to convert Local Storage string to integer using JSON?

GameCoderI am trying to retrieve a local storage string but I want it retrieved as a number, not as this string: "\"5\"". So here is my Javascript code: function numberboxFunction() { var pasteitem = document.getElementById("pastegood").innerHTML; var pasteprice = document.getElementById("pastePrice").i...

why are you stringifying it in the first place?
The .toFixed() converts into string...
did you tried pasreInt() after getting from json which convert it into integer
@charlietfl LocalStorage cannot have arrays or objects...
@PraveenKumar OP isn't passing an array or object, just an input value
09:17
Number(), Parse Int() is not working.
localestroage accepte number it doesnot takes array
@VickyKumar I don't understand your comment
@GameCoder if you store number in local storage it accepts and stores it and when you retrieve it ,it returns string
All I am trying to do is a pass a variable with a number in it.
you can pass a number to setItem() and it will automatically be stored as string. localStorage only stores strings
09:17
Use var brushnum = parseFloat(document.getElementById("brushnumber").value); and do the same for pastenum.
@Mottie I'll try.
@Mottie Not working.
@charlietfl Because Local Storage doesn't accept numbers, only strings.
@VickyKumar But how do I convert it back to a number?
ummm didn't I just say that above? You are wrapping extra quotes around it by using stringify and all you need to do is pass it in directly and cast to number when you pull it out. A quick web search for turning string to number will give you a multitude of ways to do it
@charlietfl I know that numbers are stored as strings but how do I pass it back as a number?
@charlietfl first thing is that store number as it it donot stringify and when you retrieve it use parseInt() or parseFloat()
use google and will find all sorts of simple ways to do it...there are quite a few
09:17
@charlietfl The problem is that it is not me who is doing that is local storage. I will copy the warning where the string is found: The specified value "\"5\"" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?. As you can see it is not me who is creating that extra pair of double quotes.
it is you when you use JSON.stringify() which is pointless when all you need to do is pass in number to setItem()
Number(JSON.parse(valueFromLocalStorage)) should work. But there is no need to use JSON in your case anyway, so why not just do localStorage.setItem("Shopping cart", pastenum ); and Number(localStorage.getItem('Shopping cart'))?
@FelixKling Number() is not working.
user663031
Please create a minimal version of a dozen lines or so.
@torazaburo Of what?
user663031
09:17
A minimal version of your code. See Minimal, Complete, and Verifiable example.

last day (14 days later) »