ionic : - convert string to latitude, longitude append more digit at end? - javascript

i want to convertstring to latlng suppose string is 23.097278,72.446139 i want to convert it to latlng, when i split strings i get result as i want but when i converting to latlng it appends some digits at end of co ordinate
for example
centerPoint : (23.081295, 72.49794399999996), what i have done so far is below plz help me to solve it.
temp="23.097278,72.446139";
let temp1 = temp.split(',');
let coordinatesLat = temp1[0];
let coordinatesLong = temp1[1];
console.log("temp1 : " +coordinatesLong);
let centerPoint = new google.maps.LatLng(coordinatesLat, coordinatesLong);
console.log("centerPoint : " +centerPoint);
output in log
temp1 : 72.497944
centerPoint : (23.081295, 72.49794399999996)

Try to convert it to float
var latDouble = parseFloat(lat);
var longDouble = parseFloat(long);

Related

Javascript Regex giving NaN from string

I am trying to get only numbers from starting of a string.
i have following code to get numbers and text from a string.
var unitData = '(135 g)' // it may be 0.135 or .135
var unitValue = Number(unitData.match(/.?\d+\.?\d*/).toString());
var unitName = unitData.match(/[A-Za-z]+/g) || '';
console.log(unitValue);
console.log(unitName);
its giving NaN for unitValue. It works fine if number are on first position of a string.
There is one small mistake that bracket is also picked in your regex.
It was basically returning this:
'(135
I have updated the regex. Please try below :
var unitData = '(135 g)'
var unitValue = Number(unitData.match(/\d*\.?\d+/)[0]);
console.log(unitValue);
unitData = '(.135 g)'
unitValue = Number(unitData.match(/\d*\.?\d+/)[0]);
console.log(unitValue);
unitData = '(135.42 g)'
unitValue = Number(unitData.match(/\d*\.?\d+/)[0]);
unitName = unitData.match(/[A-Za-z]+/g) || '';
console.log(unitValue);
console.log(unitName);
Hope this helps :)

Wrong Math Algorithm with javascript?

im trying to make some Algorithm function with javascript and get some problems
function Algorithm() {
var endone;
var endtwo;
var endtheend;
var v1 = document.getElementsByName("v1")[0].value; //worth to 91
var v2 = document.getElementsByName("v2")[0].value; //worth to 61
var v3 = document.getElementsByName("v3")[0].value; //worth to 20
endone = Math.round(v1 * 0.30);
endtwo = Math.round(((v2 + v3) / 2) * 0.70);
endtheend = endone + endtwo;
document.getElementById("ending").innerHTML = "end : " + endtheend;
}
if im doing the same Algorithm with a calculator im getting 55.65 , but when im trying to use this function somehow im getting 2169.
someone might know what is the problem and show me how to solve her?
The problem is that v1, v2 and v3 are not numbers. They are strings. So each calculation you make is relies on implicit conversions and operations between strings.
For instance, in the following snippete we have an implicit conversion of the the string value "91" to a double floating number and then the usual mulitplication is done.
var v1 = "91";
console.log(v1*0.3);
On the other hand below:
var v2 = "61";
var v3 = "20";
console.log((v2 + v3) / 2)
We have a string concatenation "61"+"20" results in a new string "6120" and then "6120" is implicitly converted to a double floating number and the division with 2 is done.
What's the solution ?
You have to parse these values either by using parseInt or parseFloat, like below:
var v2 = "61";
var v3 = "20";
console.log((parseInt(v2,10) + parseInt(v3,10)) / 2)
When you get an HTMLInputElement's value property, what you get is a string.
And the + operator applied to two strings merely concatenates them, so if for instance v2 == "7" and v3 === "0", when you do (v2 + v3), you'll get "70".
The solution to your problem is to simply pass the values through parseInt:
var v1 = parseInt(document.getElementsByName("v1")[0].value, 10);
var v2 = parseInt(document.getElementsByName("v2")[0].value, 10);
var v3 = parseInt(document.getElementsByName("v3")[0].value, 10);
// The second argument to parseInt isn't needed if you only target newer browsers.
I'd suggest you read up on type coercion in JavaScript for more info.
The issue is all of your values (v1, v2, v3) are String type. You need to convert them into Number first. So the following code should work :
function Algorithm() {
var endone;
var endtwo;
var endtheend;
var v1 = Number(document.getElementsByName("v1")[0].value);
var v2 = Number(document.getElementsByName("v2")[0].value);
var v3 = Number(document.getElementsByName("v3")[0].value);
endone = Math.round(v1 * 0.30);
endtwo = Math.round(((v2 + v3) / 2) * 0.70);
endtheend = endone + endtwo;
document.getElementById("ending").innerHTML = "end : " + endtheend;
}
you can also use parseInt if your value contains any alphabetic characters.

How do I extract and split coordinates from jQuery values inside brackets?

I'm getting coordinates via jQuery like this and fill them into a form:
$('#location').val(pos);
The problem is that the value is filled in like this:
(40.00000, 150.00000)
How do I extract them from the brackets and "split" them into latitude & longitude values like:
pos_lat = 40.00000;
pos_long = 150.00000;
https://jsfiddle.net/ojcoj74y/
var pos = "(40.00000, 150.00000)";
var pos_segs = pos.slice(1,-1).split(', ');
var pos_lat = pos_segs[0];
var pos_long = pos_segs[1];
UPDATE:
Thanks! Is it possible to run this within a function, too? –
user1996496 1 hour ago
https://jsfiddle.net/ojcoj74y/1/
function getPos(strPos) {
var pos_segs = strPos.slice(1, -1).split(', ');
return {
posLat: pos_segs[0],
posLong: pos_segs[1]
};
}
Remove brackets and empty space and then split by comma. Finally (if you need) parse strings to floats:
positionString.replace(/\(|\)|\s/g, '').split(',')).map(parseFloat);

JS parseFloat creates Int when parsing ASP.NET MVC model data

I want to parse Model data as a float to Javascript variable, but I will always get no decimals
function initMap() {
var latitude = parseFloat("#Model.latitude");
var longitude = parseFloat("#Model.longitude");
console.log(latitude + " " + longitude);
}
For example, if Model.latitude = 46.3245 I will get 46.
I have also tried this: var latitude = parseFloat("#Model.latitude").toFixed(4); but I get 46.0000
What can I do ?

Convert String to Float in Javascript error

Please see below code,i getting wrong value.
eg;
var FirstValue=0.00;
var secondvalue=parseFloat("22.88",10).toFixed(2);
var thirdvalue=(FirstValue) + (secondvalue);
am getting value like"22.8822.88"
Please help me to solve.Its not convert to numeric .
toFixed convert you float value to string back. So when you adding two values you will get not number addition but string concatenation:
2.0 + 2.0 = 4.0 // number
"2.0" + "2.0" = "2.02.0" // string
Remove to fixed after conversion. Than add two values and than do to fixed:
var FirstValue=0.00;
var secondvalue=parseFloat("22.88",10);
var thirdvalue= ( (FirstValue) + (secondvalue) ).toFixed(2);
try this:
var FirstValue=0.00;
var secondvalue=parseFloat("22.88");
secondvalue = parseFloat(secondvalue.toFixed(2));
var thirdvalue=(FirstValue) + (secondvalue);
toFixed returns a string, not a number so it needs to be converted again.

Categories

Resources