Andy Jacobs
Posts: 0
Joined: Thu Apr 16, 2015 11:34 am

localStorage to integer

Ok, I've scoured the net, tried every combination I can think of and now I'm going to go mad!

I've got a localStorage variable called SR which is input from a form.

I want to turn this into an integer.

When I use the console to examine the DOM, I can see:

""12345""

So it's definitely there but being stored as a string.

I've then got:

var SR = parseInt(localStorage.SR);
alert (SR);

The alert box just comes up with NaN

If anyone has any ideas it would save my sanity :)

Regards
Andy

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

localStorage to integer

Hello,

To get value stored in localStorage use this code
prelocalStorage.getItem('name');/pre

where name - name of localStorage variable

Andy Jacobs
Posts: 0
Joined: Thu Apr 16, 2015 11:34 am

localStorage to integer

Hi Alena

Thanks for the reply.

But that gives me a string.

I need to do some maths on several localStorage variables.

RobertJay
Posts: 0
Joined: Fri Jun 15, 2012 1:32 pm

localStorage to integer

Andy, perhaps this might work:

var t1 = localStorage.getItem('name');
var t2 = Number(t1);

Andy Jacobs
Posts: 0
Joined: Thu Apr 16, 2015 11:34 am

localStorage to integer

Hi Robert

Thanks for that. I'm sure I've tried something similar but this is what I've added now:

var t1 = localStorage.getItem('SR');
var t2 = Number(t1);
localStorage.setItem('REBATE', t2 * 0.86 /100);

This comes up with NaN as well.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

localStorage to integer

Hello,

Could you check, what values do variables t1 and t2 have?
E.g.:

prealert(t1);
alert(t2);/pre

Andy Jacobs
Posts: 0
Joined: Thu Apr 16, 2015 11:34 am

localStorage to integer

Hi Sergiy

Thanks for the reply. I think I may have a solution. I have this working using your API but I wanted to avoid using this as I assumed that this would rely on an internet connection. This project specifies that the app must work offline. One of your colleagues has told me that I can use the API and that some magic will happen when I package it as an APK or IPA file.

If this is definitely the case then this is perfect!

Best regards
Andy

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

localStorage to integer

If your app doesn't use appery.io DB, you can just export it and use without internet connection. If it uses local DB, please follow this tutorial: https://devcenter.appery.io/tutorials...

Return to “Issues”