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?










share|improve this question




















  • 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 as parseInt(), only for floats. Float is a type of data that supports decimal points.
    – fingeron
    Nov 22 at 14:09















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?










share|improve this question




















  • 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 as parseInt(), only for floats. Float is a type of data that supports decimal points.
    – fingeron
    Nov 22 at 14:09













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 14:24

























asked Nov 22 at 14:04









rob.m

3,629103782




3,629103782








  • 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 as parseInt(), only for floats. Float is a type of data that supports decimal points.
    – fingeron
    Nov 22 at 14:09














  • 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 as parseInt(), 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












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.






share|improve this answer























  • 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


















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");








share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer























  • 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















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.






share|improve this answer























  • 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 same Number() one.
    – Jai
    Nov 22 at 14:14


















  • 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
















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












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");








share|improve this answer























  • 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















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");








share|improve this answer























  • 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













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");








share|improve this answer














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");






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu