I got a variable Javascrpit which has a number as a string in this case 0.84. I'm trying to convert it into a float but when I try to it appears a 0 as float instead the 0.84.
I'm using this:
var pot="0.84";
var asd = parseFloat(pot);
console.log(asd);
EDIT:
This is not exactly the example. I recover data from the HTML and it works for other numbers but not for this. It is difficult to explain my problem exactly. It is a lot of code and works for other numbers so don't know exactly.
Your input is not "0.84". If you test with that, you will get the correct answer. Your input has something else inside, like spaces, for example:
"0 .84"
This should be the solution:
parseFloat(pod.replace(/ /g, ""))
I have tried this example on my end and it completely worked. However, you can try to instead input the string value directly into the parse float() function and it should print our your expected value. If you still want to assign the parsefloat() to a variable, then try to either rewrite the code or re-open your IDE because the code should work.
var pot = "0.84"
console.log(parseFloat(pot))
or you can just write it in one line
console.log(parseFloat("0.84"))
Related
I'm having an issue trying to set an input field's value when using HTML entities in that they are coming out literally as " rather than ".
Here is the code I am using:
document.getElementById('inputSurname').setAttribute('value', 'test"""');
in which the output is test""" though I want the output to be test""".
It doesn't look like a double-encoding issue since in the source code I am seeing it the same way I have set it here.
I know I could decode the value from its HTML entity format though this is something I want to avoid if possible for security.
Any help would be much appreciated :)
Try this:
document.getElementById('inputSurname').value = 'test"""';
Or if you want to keep ":
function myFunction() {
document.getElementById('myText').value = replaceQuot('test"""');
}
function replaceQuot(string){
return string.replace('"', '""');
}
Or you can use escape characters.
document.getElementById("inputSurname").setAttribute("value", "test\"\"\"\"");
Well you could just write the new value as 'test"""'.
For other characters however, I'm going to refer you to this answer: HTML Entity Decode
I'm trying to convert a currency string to a number. I'm using a replace function with a regexp that I've used successfully in a similar context before.
The currency string is captured here, in part of an "each" loop:
var unitGridPrice = jQuery(this).find(".clsPriceGridDtlPrice").html();
The result is that unitGridPrice is a currency string, something like "$2.75". I'm trying to convert it to a number here:
var priceToConvert = unitGridPrice;
var unitGridPriceNo = Number(priceToConvert.replace(/[^0-9\.]+/g, ''));
However with that last line in place, the script will not run.
If I use the value of priceToConvert it correctly displays the currency text string, so I believe the string feeding the replace function is correct.
if I change "var priceToConvert = unitGridPrice" to "var priceToConvert = "$2.75" the script properly returns 2.75. I can copy and past the value that unitGridPrice displays into the text string I'm testing with and it works, but with the variable there the script dies.
I've tried removing the regex, changing the replace to .replace('$', '') and again the script stops with the variable in place but works if I test with a fixed string.
I'm really stumped. Help??!! Thank you!!!
i had some problem while try to get number from string also, little time ago. the problem is the regex, so i changed the regex like code below.
var id = element.name.replace ( /[^\d.]/g, '' );
element.name above is like input_21,input_22, etc. and i wanna get only the number(21,22).
hope it can help you. :)
I have an equation/formula stored in database and I want it to be triggered based on key up input event in a webpage.
Example formula: [55-57]
This is a simple minus operation, where the number actually represents the id of a row in database
I have looked at this solution which replaces numbers found in a string to new value. But I need the new value to be replaced with incremented letters such as a, b and so on. Also the leading and ending brackets [] need to be removed so that I can perform an eval later using JavaScript.
Later the equation will be convert to a-b. Variable a and b represent other HTML elements that holds a value. So whenever I key in something into text field, changes will reflect on other part of webpage. It's like auto computation.
Thank you for those helping this. Hope this question will help somebody.
Try something like this. If you need more help, you seriously need to re-word your question or post a jsfiddle, or something.
var eqn = '55-57'; // brackets removed. Remove them with a regex of /\[|\]/g if you need to
var result = eval( eqn.replace( /\w+/g, function( res ){
return +document.getElementById( res[1] );
} );
Basically this replaces 55 and 57 with the numerical values of #55 and #57. It would also work for #b, etc.
It then eval's the result, basically doing whatever math is in your equation.
I'm cobbling together a script which maps mouse X,Y coordinates to an axis grid. The resulting variable will then be passed to CSS transform property. I'm getting all the numbers I need, but I'm stuck on the last part, which is to remove the \ around the result, which has been converted to a regular expression so as to allow for negative integers.
var resultX = RegExp(Math.round(mousePos.x/6.6) -60);
resultX = resultX.replace(/\//g,'');
The final stage (stripping the slashes) throws an error no matter how I do it. I've tried encapsulating .replace in a function, and using return, but I continue to get the same error:
TypeError: 'undefined' is not a function (evaluating
'resultX.replace(///g,'')')
I'm stuck, and haven't been able to find the solution anywhere. Perhaps the problem is that my variable isn't a true string? Or maybe someone has a suggestion for a better way to allow for negative integers.
JS fiddle:
http://jsfiddle.net/wAKnY/
Like JayC mentioned in the comments, there doesn't seem to be a reason to convert to a RegExp in the first place, so I would just recommend removing the enclosing RegExp().
However, if you require that for some reason that isn't apparent here, you can then call toString() on it to enable the replace function to behave correctly:
resultX = resultX.toString().replace(/\//g,'');
As JayC said, you don't need a regexp in the first place.
var resultX = (Math.round(mousePos.x/6.6)-60).toString;
This works:
resultX = resultX.toString().replace(/\//g, '');
resultY = resultY.toString().replace(/\//g, '');
thanks for looking my questions, I can't find the way to remove the comma with javascript. So I have problem at removing comma from page with java-script, Adding Comma for output is fine i guess, but for removing comma for value, the function is not working for me.
The source code is : http://jsfiddle.net/Q5CwM/2/
Please let me know, thanks again.
I have no idea what your code is trying to do overall, but you can fix this function that removes commas:
function checkNumeric(objName) {
var lstLetters = objName;
var lstReplace = lstLetters.replace(/\,/g,'');
}
by changing it to this:
function removeCommas(str) {
return(str.replace(/,/g,''));
}
You weren't returning the changed string from the function and I changed the name of the function and parameters to represent what it does.
str.replace returns the changed string. It does not change the string you started with so in order to do something with the result of the replacement, you have to either return that from the function or assign it to some other string. As you had it, nothing was happening with the replaced string so the function did nothing.