.toFixed is not working in my code. I am using it with .toLocaleString()
JS / Fiddle: https://jsfiddle.net/8b6t90f5/
$(function() {
var value = 5000.3269588;
$("#process").click(function() {
$('#amount').text("Total: $" + value.toLocaleString().toFixed(2));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="amount"></div>
<input id="process" class="button_text" type="submit" name="submit" value="SHOW VALUE">
Strings do not have a toFixed(), only numbers do.
$('#amount').text("Total: " + value.toLocaleString("en-US", {maximumFractionDigits:2, currency:"USD", style:"currency"}));
is possibly what you're after.
toFixed() is a Number method. toLocaleString() turns it into a string. You need to use toFixed() first, then parse that back to float and use toLocaleString():
parseFloat(value.toFixed(2)).toLocalString('en-BR');
const num = 50023.357289357;
console.log(parseFloat(num.toFixed(2)).toLocaleString());
You continue to use string methods for numbers and vice versa. Try this:
const num = 5000.3269588;
console.log(num.toLocaleString(undefined, {maximumFractionDigits: 2}));
Related
I'm trying to validate the decimal number pattern with specific length but no luck.
I tried this pattern="[0-9]){1,2}(\.){1}([0-9]){2}" but this works only 12.12
I'm looking for (13digits).(6digits) pattern validation and length validation.
Expected is,
`1234567891123.123456` //true
`1234567891123123456` //false since only number
`12345678911234.123456` //false since 14digits.6digits
`1234567891123.1234567` //false since 13digits.7digits
What be the better regx to fulfill the above validations?
A useful site for regex testing is https://regexr.com/
\b\d{13}\.\d{6}\b
The regex that you need should be like this:
([0-9]){13}(\.){1}([0-9]){6}
So for example in HTML form input, it would look like this:
<form action="">
<input type="text" required pattern="([0-9]){13}(\.){1}([0-9]){6}" />
<button type='submit'>Submit</button>
</form>
You can try using this:
\d{13}[.]\d{6}
Working Demo :
var regex = /^[0-9]{13}\.[0-9]{6}$/i;
function getValue() {
return document.getElementById("myinput").value;
}
function test() {
alert(regex.test(getValue()));
}
<input type="text" id="myinput"/>
<button id="testBtn" onclick=test()>test</button>
I want to have a program that turns "-" into an actual minus sign the computer will recognize.
Here is my code:
document.getElementById("answer").innerHTML = (Number(input.substr(Number(input.indexOf("+"))).slice(1)) + Number(input.substr(0, Number(input.indexOf("+")))));
I take it you're trying to build a calculator. I'd personally create different functions for adding, subtracting, etc. You could use eval(), but that's prone to XSS.
Here's a simple calculator showcasing the minus pre-built into the JavaScript calculation, so that you don't have to worry about extracting it from the input:
function add() {
document.getElementById('answer').innerHTML = Number(document.getElementById('input1').value) + Number(document.getElementById('input2').value);
}
function subtract() {
document.getElementById('answer').innerHTML = document.getElementById('input1').value - document.getElementById('input2').value;
}
<input id="input1">
<input id="input2">
<button onclick="add()">Add</button>
<button onclick="subtract()">Subtract</button>
<div id="answer"></div>
Hope this helps! :)
This is the code:
<html>
<body>
<script>
function myFunction(var1,var2){
number=var1+var2
document.write(number)
}
</script>
<form>
Number 1 : <input type="text" name="no1"><br>
Number 2 : <input type="text" name="no2"><br>
<input type="button" onclick="myFunction(this.form.no1.value,this.form.no2.value)" value="submit">
</form>
<p id="demo></p
</body>
</html>
When I insert 10 for number 1 and 20 for number 2, the output is:
1020
But i want it to display 30.
What can i do?
**I have tried myFunction(10,20), the result is 30.
simply use parse the variable value to integer using parseInt() method or add "+"before to your variable name. Because variables var1 and var2 returning string. To calculate those variable values, you need to convert it as a integer.
using parseInt() method
number=parseInt(var1)+parseInt(var2)
use + before variable name to convert into integer,
number= +var1 + +var2
try this code,
<html>
<body>
<script>
function myFunction(var1,var2){
number = parseInt(var1) + parseInt(var2)
//another way number= +var1+ +var2
document.write(number)
}
</script>
<form>
Number 1 : <input type="text" name="no1"><br>
Number 2 : <input type="text" name="no2"><br>
<input type="button" onclick="myFunction(this.form.no1.value,this.form.no2.value)" value="submit">
</form>
<p id="demo"></p>
</body>
</html>
using parseInt() DEMO
using + before variable name DEMO
modify your function with parseInt like:
<script>
function myFunction(var1,var2){
number=parseInt(var1)+parseInt(var2);
document.write(number);
}
</script>
You were getting output like 1020 because by default data from the textbox is taken as text type, so we need to convert it to Number Type first, for that we are using parseInt(for explicit conversion)
Your javascript thinks you are appending strings... To make sure your javascript knows it's numbers your working with you need to convert it to that type.
<html>
<body>
<script>
function myFunction(var1, var2){
number = parseInt(var1, 10) + parseInt(var2, 10)
document.write(number)
}
</script>
<form>
Number 1 : <input type="text" name="no1"><br>
Number 2 : <input type="text" name="no2"><br>
<input type="button" onclick="myFunction(this.form.no1.value,this.form.no2.value)" value="submit">
</form>
<p id="demo"></p>
</body>
</html>
For more info about parseInt check this documentation.
Update your method to
function myFunction(var1,var2){
number=parseInt(var1) + parseInt(var2)
document.write(number)
}
As this.form.no1.value is returning a string, so both the numbers are concatenated as strings instead of summing up as numbers.
Two options:
Change your input tag to
<input type="button" onclick="myFunction(parseInt(this.form.no1.value, 10),parseInt(this.form.no2.value, 10))" value="submit">
OR
Change your JavaScript function to
function myFunction(var1,var2){
var number=parseInt(var1, 10)+parseInt(var2, 10);
document.write(number);
}
It is because the values you extract from your input fields are strings. When you add two strings, they are usually concatenated. Try looking at the javascript method parseIntas Evan suggests in the comments or look at parseFloatif you want to allow floats.
parseFloat docs
Your method would then look like this:
function myFunction(var1,var2){
number = parseFloat(var1) + parseFloat(var2)
document.write(number)
}
It's now just string concatenation. Please use "parseInt()" to get the result.
thanks.
Your not doing a calculation, you are appending two Strings. In order to calculate the mathematical answer for var1 + var2 you should parse them to Integers.
result = parseInt(var1) + parseInt(var2);
I have a question regarding the add sign in JavaScript I'm a bit confused on this. I have this input text box which I will be input as 50 and it will add plus 50 . My result in adding the numbers which for example I input 50 the result is 5050 which is totally wrong. Can someone help me on this?
<!DOCTYPE html>
<html>
<head>
<title>activity 2</title>
<script type="text/javascript">
function computeSalary(){
var salaryData = document.form1.salary.value;
var salary1 = salaryData + 50;
document.form1.newSalary.value = salary1;
}
</script>
</head>
<body>
<form name="form1">
Enter the daily salary:
<input type="text" name="salary" /><br />
<input type="button" value="Compute" onClick="computeSalary();" /><br />
<br />
The new salary: <input type="text" name="newSalary" />
</form>
</body>
</html>
You can convert the string value you're getting, which is being concatenated to your value, to a number by simply adding a plus sign:
var salary1 = +salaryData + 50;
jsFiddle example
You have to convert the value you got from the input control to float or integer before adding it using the + operator. The + operator will convert both operands to a same type before adding both operands. This is the primary reason why you got 5050 because the 50 of type int got converted to string.
use this code:
function computeSalary(){
var salaryData = document.form1.salary.value;
var salary1 = parseFloat(salaryData) + 50;
document.form1.newSalary.value = salary1;
}
Change the following line:
var salary1 = parseInt(salaryData) + 50; // Use parseInt or parseFloat
The reason is that JavaScript will coerce strings and integers into strings. So your integer 50 is converted to a string and then concatenated.
How do I compare similar strings in jquery?
<script src="../../js/jq.js"></script>
<script>
$(function(){
var str1 = $.trim($('#str1').val().toUpperCase());
var str2 = $.trim($('#str2').val().toUpperCase());
if(str1==str2){
console.log('yep');
}
});
</script>
<input type="text" id="str1" value="One String"/>
<input type="text" id="str2" value="One String1"/>
Comparing "One String" and "One String1" is not going to work if I'm only checking if the two values are equal. Is there any way to do this in jquery? For example I only want to compare 90% of the string.
You can see if one is contained inside the other for example:
if (str1.indexOf(str2) >= 0 || str2.indexOf(str1) >= 0)
console.log('yep');
}
Check this out : http://jsfiddle.net/Sj5dE/ You can comment out a,b block to see the similitude of the strings. It's of course case-sensitive. I hope this helps. Since your example talked about comparing two similar strings, I thought it'd be most likely that the beginning of the strings are the same so I didn't introduce any substring logic but feel free to change the function.
Try this it might work
<script src="../../js/jq.js"></script> <script>
$(function(){
var str1 = $.trim($('#str1').val().toUpperCase());
var str2 = $.trim($('#str2').val().toUpperCase());
if(str1===str2){ console.log('yep'); } });
</script>
<input type="text" id="str1" value="One String"/>
<input type="text" id="str2" value="One String1"/>
In javascript you can use the substring to get the 90% of the string you would like to compare.
$(function(){
var str1 = $.trim($('#str1').val().toUpperCase().substring(0, 10);
var str2 = $.trim($('#str2').val().toUpperCase().substring(0, 10);
if(str1==str2){
console.log('yep');
}
look on
http://code.google.com/p/google-diff-match-patch/
the demo in
http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html
You can check if the string is present or not by
$(".className").replace(/(^|\s)yourTextHere\S+/g, " ");