Page 1 of 1

localStorage to integer

Posted: Fri Apr 17, 2015 10:02 am
by Andy Jacobs

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


localStorage to integer

Posted: Fri Apr 17, 2015 1:28 pm
by Alena Prykhodko

Hello,

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

where name - name of localStorage variable


localStorage to integer

Posted: Fri Apr 17, 2015 1:31 pm
by Andy Jacobs

Hi Alena

Thanks for the reply.

But that gives me a string.

I need to do some maths on several localStorage variables.


localStorage to integer

Posted: Fri Apr 17, 2015 1:36 pm
by RobertJay

Andy, perhaps this might work:

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


localStorage to integer

Posted: Fri Apr 17, 2015 1:59 pm
by Andy Jacobs

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.


localStorage to integer

Posted: Fri Apr 17, 2015 7:39 pm
by Serhii Kulibaba

Hello,

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

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


localStorage to integer

Posted: Sat Apr 18, 2015 7:15 am
by Andy Jacobs

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


localStorage to integer

Posted: Sat Apr 18, 2015 1:21 pm
by Serhii Kulibaba

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...