How to convert string with comma to numbers?
up vote
-1
down vote
favorite
I have some coordinates in a custom field:
45.4924038,9.2040718
And I am loading them into a map like this:
var mylatLg = "<?php echo get_post_meta($id, 'usp-custom-90', true); ?>";
var mylatLg = mylatLg.split(',');
console.log(mylatLg[0]);
console.log(mylatLg[1]);
var lat = parseInt(mylatLg[0]);
var lng = parseInt(mylatLg[1]);
The first console.log gives:
45.4924001
9.206260499999985
The second control.log says:
45
9
If I load the markers as per the following below, it gives me a wrong position (as it is 45,9)
var marker = new google.maps.Marker({
map: map,
position: {lat: parseInt(mylatLg[0]), lng: parseInt(mylatLg[1])},
If I load the markers as per the following below
var marker = new google.maps.Marker({
map: map,
position: {lat: mylatLg[0], lng: mylatLg[1]},
It gives
InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in
property lat: not a number
How can I run 45.4924038,9.2040718
as I have in the custom field?
javascript google-maps
|
show 2 more comments
up vote
-1
down vote
favorite
I have some coordinates in a custom field:
45.4924038,9.2040718
And I am loading them into a map like this:
var mylatLg = "<?php echo get_post_meta($id, 'usp-custom-90', true); ?>";
var mylatLg = mylatLg.split(',');
console.log(mylatLg[0]);
console.log(mylatLg[1]);
var lat = parseInt(mylatLg[0]);
var lng = parseInt(mylatLg[1]);
The first console.log gives:
45.4924001
9.206260499999985
The second control.log says:
45
9
If I load the markers as per the following below, it gives me a wrong position (as it is 45,9)
var marker = new google.maps.Marker({
map: map,
position: {lat: parseInt(mylatLg[0]), lng: parseInt(mylatLg[1])},
If I load the markers as per the following below
var marker = new google.maps.Marker({
map: map,
position: {lat: mylatLg[0], lng: mylatLg[1]},
It gives
InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in
property lat: not a number
How can I run 45.4924038,9.2040718
as I have in the custom field?
javascript google-maps
2
Did you try withparseFloat()
?
– theapologist
Nov 22 at 14:06
what does it do? @LloydFrancis
– rob.m
Nov 22 at 14:07
@LloydFrancis I don't want to conver it to an integer, I want to keep the correct coords, maybe stringify?
– rob.m
Nov 22 at 14:08
1
So instead of parsing your values as a number, it parses them as floats. That means you can use decimal points. Just asking, do you have to parse them at all? Won't using it without parsing work? ( I am not familiar with the Maps JS API, just a question)
– theapologist
Nov 22 at 14:08
1
parseFloat()
is same asparseInt()
, only for floats. Float is a type of data that supports decimal points.
– fingeron
Nov 22 at 14:09
|
show 2 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have some coordinates in a custom field:
45.4924038,9.2040718
And I am loading them into a map like this:
var mylatLg = "<?php echo get_post_meta($id, 'usp-custom-90', true); ?>";
var mylatLg = mylatLg.split(',');
console.log(mylatLg[0]);
console.log(mylatLg[1]);
var lat = parseInt(mylatLg[0]);
var lng = parseInt(mylatLg[1]);
The first console.log gives:
45.4924001
9.206260499999985
The second control.log says:
45
9
If I load the markers as per the following below, it gives me a wrong position (as it is 45,9)
var marker = new google.maps.Marker({
map: map,
position: {lat: parseInt(mylatLg[0]), lng: parseInt(mylatLg[1])},
If I load the markers as per the following below
var marker = new google.maps.Marker({
map: map,
position: {lat: mylatLg[0], lng: mylatLg[1]},
It gives
InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in
property lat: not a number
How can I run 45.4924038,9.2040718
as I have in the custom field?
javascript google-maps
I have some coordinates in a custom field:
45.4924038,9.2040718
And I am loading them into a map like this:
var mylatLg = "<?php echo get_post_meta($id, 'usp-custom-90', true); ?>";
var mylatLg = mylatLg.split(',');
console.log(mylatLg[0]);
console.log(mylatLg[1]);
var lat = parseInt(mylatLg[0]);
var lng = parseInt(mylatLg[1]);
The first console.log gives:
45.4924001
9.206260499999985
The second control.log says:
45
9
If I load the markers as per the following below, it gives me a wrong position (as it is 45,9)
var marker = new google.maps.Marker({
map: map,
position: {lat: parseInt(mylatLg[0]), lng: parseInt(mylatLg[1])},
If I load the markers as per the following below
var marker = new google.maps.Marker({
map: map,
position: {lat: mylatLg[0], lng: mylatLg[1]},
It gives
InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in
property lat: not a number
How can I run 45.4924038,9.2040718
as I have in the custom field?
javascript google-maps
javascript google-maps
edited Nov 22 at 14:24
asked Nov 22 at 14:04
rob.m
3,629103782
3,629103782
2
Did you try withparseFloat()
?
– theapologist
Nov 22 at 14:06
what does it do? @LloydFrancis
– rob.m
Nov 22 at 14:07
@LloydFrancis I don't want to conver it to an integer, I want to keep the correct coords, maybe stringify?
– rob.m
Nov 22 at 14:08
1
So instead of parsing your values as a number, it parses them as floats. That means you can use decimal points. Just asking, do you have to parse them at all? Won't using it without parsing work? ( I am not familiar with the Maps JS API, just a question)
– theapologist
Nov 22 at 14:08
1
parseFloat()
is same asparseInt()
, only for floats. Float is a type of data that supports decimal points.
– fingeron
Nov 22 at 14:09
|
show 2 more comments
2
Did you try withparseFloat()
?
– theapologist
Nov 22 at 14:06
what does it do? @LloydFrancis
– rob.m
Nov 22 at 14:07
@LloydFrancis I don't want to conver it to an integer, I want to keep the correct coords, maybe stringify?
– rob.m
Nov 22 at 14:08
1
So instead of parsing your values as a number, it parses them as floats. That means you can use decimal points. Just asking, do you have to parse them at all? Won't using it without parsing work? ( I am not familiar with the Maps JS API, just a question)
– theapologist
Nov 22 at 14:08
1
parseFloat()
is same asparseInt()
, only for floats. Float is a type of data that supports decimal points.
– fingeron
Nov 22 at 14:09
2
2
Did you try with
parseFloat()
?– theapologist
Nov 22 at 14:06
Did you try with
parseFloat()
?– theapologist
Nov 22 at 14:06
what does it do? @LloydFrancis
– rob.m
Nov 22 at 14:07
what does it do? @LloydFrancis
– rob.m
Nov 22 at 14:07
@LloydFrancis I don't want to conver it to an integer, I want to keep the correct coords, maybe stringify?
– rob.m
Nov 22 at 14:08
@LloydFrancis I don't want to conver it to an integer, I want to keep the correct coords, maybe stringify?
– rob.m
Nov 22 at 14:08
1
1
So instead of parsing your values as a number, it parses them as floats. That means you can use decimal points. Just asking, do you have to parse them at all? Won't using it without parsing work? ( I am not familiar with the Maps JS API, just a question)
– theapologist
Nov 22 at 14:08
So instead of parsing your values as a number, it parses them as floats. That means you can use decimal points. Just asking, do you have to parse them at all? Won't using it without parsing work? ( I am not familiar with the Maps JS API, just a question)
– theapologist
Nov 22 at 14:08
1
1
parseFloat()
is same as parseInt()
, only for floats. Float is a type of data that supports decimal points.– fingeron
Nov 22 at 14:09
parseFloat()
is same as parseInt()
, only for floats. Float is a type of data that supports decimal points.– fingeron
Nov 22 at 14:09
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You need to use parseFloat
and not parseInt
. This will parse the strings to floats.
Or just try to cast it using Number
, e.g. Number('9.206260499999985')
will produce the float 9.206260499999985.
yes works too as the other answer does too
– rob.m
Nov 22 at 14:11
That seems good to me. also about to post the sameNumber()
one.
– Jai
Nov 22 at 14:14
add a comment |
up vote
4
down vote
Instead try with +
unary operator which somewhat works like parseFloat()
:
var lat = +mylatLg[0];
var lng = +mylatLg[1];
console.log(+"45.4924001");
console.log(+"9.206260499999985");
perfect, it works. What does it do tho? I didn't know
– rob.m
Nov 22 at 14:09
1
FYI parseint-vs-unary-plus will help you on understanding.
– Jai
Nov 22 at 14:13
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You need to use parseFloat
and not parseInt
. This will parse the strings to floats.
Or just try to cast it using Number
, e.g. Number('9.206260499999985')
will produce the float 9.206260499999985.
yes works too as the other answer does too
– rob.m
Nov 22 at 14:11
That seems good to me. also about to post the sameNumber()
one.
– Jai
Nov 22 at 14:14
add a comment |
up vote
3
down vote
accepted
You need to use parseFloat
and not parseInt
. This will parse the strings to floats.
Or just try to cast it using Number
, e.g. Number('9.206260499999985')
will produce the float 9.206260499999985.
yes works too as the other answer does too
– rob.m
Nov 22 at 14:11
That seems good to me. also about to post the sameNumber()
one.
– Jai
Nov 22 at 14:14
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You need to use parseFloat
and not parseInt
. This will parse the strings to floats.
Or just try to cast it using Number
, e.g. Number('9.206260499999985')
will produce the float 9.206260499999985.
You need to use parseFloat
and not parseInt
. This will parse the strings to floats.
Or just try to cast it using Number
, e.g. Number('9.206260499999985')
will produce the float 9.206260499999985.
edited Nov 22 at 14:11
answered Nov 22 at 14:10
Pehota
1064
1064
yes works too as the other answer does too
– rob.m
Nov 22 at 14:11
That seems good to me. also about to post the sameNumber()
one.
– Jai
Nov 22 at 14:14
add a comment |
yes works too as the other answer does too
– rob.m
Nov 22 at 14:11
That seems good to me. also about to post the sameNumber()
one.
– Jai
Nov 22 at 14:14
yes works too as the other answer does too
– rob.m
Nov 22 at 14:11
yes works too as the other answer does too
– rob.m
Nov 22 at 14:11
That seems good to me. also about to post the same
Number()
one.– Jai
Nov 22 at 14:14
That seems good to me. also about to post the same
Number()
one.– Jai
Nov 22 at 14:14
add a comment |
up vote
4
down vote
Instead try with +
unary operator which somewhat works like parseFloat()
:
var lat = +mylatLg[0];
var lng = +mylatLg[1];
console.log(+"45.4924001");
console.log(+"9.206260499999985");
perfect, it works. What does it do tho? I didn't know
– rob.m
Nov 22 at 14:09
1
FYI parseint-vs-unary-plus will help you on understanding.
– Jai
Nov 22 at 14:13
add a comment |
up vote
4
down vote
Instead try with +
unary operator which somewhat works like parseFloat()
:
var lat = +mylatLg[0];
var lng = +mylatLg[1];
console.log(+"45.4924001");
console.log(+"9.206260499999985");
perfect, it works. What does it do tho? I didn't know
– rob.m
Nov 22 at 14:09
1
FYI parseint-vs-unary-plus will help you on understanding.
– Jai
Nov 22 at 14:13
add a comment |
up vote
4
down vote
up vote
4
down vote
Instead try with +
unary operator which somewhat works like parseFloat()
:
var lat = +mylatLg[0];
var lng = +mylatLg[1];
console.log(+"45.4924001");
console.log(+"9.206260499999985");
Instead try with +
unary operator which somewhat works like parseFloat()
:
var lat = +mylatLg[0];
var lng = +mylatLg[1];
console.log(+"45.4924001");
console.log(+"9.206260499999985");
console.log(+"45.4924001");
console.log(+"9.206260499999985");
console.log(+"45.4924001");
console.log(+"9.206260499999985");
edited Nov 22 at 14:17
answered Nov 22 at 14:08
Jai
63.5k95479
63.5k95479
perfect, it works. What does it do tho? I didn't know
– rob.m
Nov 22 at 14:09
1
FYI parseint-vs-unary-plus will help you on understanding.
– Jai
Nov 22 at 14:13
add a comment |
perfect, it works. What does it do tho? I didn't know
– rob.m
Nov 22 at 14:09
1
FYI parseint-vs-unary-plus will help you on understanding.
– Jai
Nov 22 at 14:13
perfect, it works. What does it do tho? I didn't know
– rob.m
Nov 22 at 14:09
perfect, it works. What does it do tho? I didn't know
– rob.m
Nov 22 at 14:09
1
1
FYI parseint-vs-unary-plus will help you on understanding.
– Jai
Nov 22 at 14:13
FYI parseint-vs-unary-plus will help you on understanding.
– Jai
Nov 22 at 14:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432708%2fhow-to-convert-string-with-comma-to-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Did you try with
parseFloat()
?– theapologist
Nov 22 at 14:06
what does it do? @LloydFrancis
– rob.m
Nov 22 at 14:07
@LloydFrancis I don't want to conver it to an integer, I want to keep the correct coords, maybe stringify?
– rob.m
Nov 22 at 14:08
1
So instead of parsing your values as a number, it parses them as floats. That means you can use decimal points. Just asking, do you have to parse them at all? Won't using it without parsing work? ( I am not familiar with the Maps JS API, just a question)
– theapologist
Nov 22 at 14:08
1
parseFloat()
is same asparseInt()
, only for floats. Float is a type of data that supports decimal points.– fingeron
Nov 22 at 14:09