This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
How to force addition instead of concatenation in javascript [duplicate]
(3 answers)
Closed 3 years ago.
I have the following error:
jquery-3.4.1.min.js:2 The specified value "24.164.83" is not a valid
number. The value must match to the following regular expression:
-?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?
Code
var grossTotal = netPrice * vatTotal // Is OK - it multiplies values
but
var grossTotal = netPrice + vatTotal // It makes this error -
it doesn’t sum.
The easiest way to produce a number from a string is prepend with +
var grossTotal= +netPrice + +vatTotal;
Try this code:
var netPrice = 1.432;
var vatTotal = 14.423;
var grossTotal = parseFloat(netPrice) + parseFloat(vatTotal)// Change according to data type of netPrice and/or vatTotal
console.log(grossTotal);
Try with type conversion:
var answer = parseInt(netPrice ) + parseInt(vatTotal);
You can use this code, First format value using parseFloat
var netPrice = 2.3777;
var vatTotal = 1.3777;
var grossTotal = parseFloat(netPrice ) + parseFloat(vatTotal);
console.log(grossTotal);
var netPrice = 2;
var vatTotal = 1;
var grossTotal = parseInt(netPrice ) + parseInt(vatTotal);
console.log(grossTotal);
For more information about parseFloat
https://www.w3schools.com/jsref/jsref_parsefloat.asp
Related
This question already has answers here:
Javascript toFixed localized?
(4 answers)
Closed last month.
Hey dear Stackoverflow community,
I have a JS code that calculates various values based on an input value. It is output with a dot, but it should be displayed with a comma.
How can I customize the code for this?
<script>
var input = document.getElementById("invest_input-1");
input.oninput = function() {
var out = ((input.value * 0.38));
document.getElementById('invest_output_result-1').innerHTML = out.toFixed(2);
var out = ((input.value * 0.032));
document.getElementById('invest_output_result-2').innerHTML = out.toFixed(2);
var out = ((input.value * 0.03));
document.getElementById('invest_output_result-3').innerHTML = out.toFixed(2);
var out = ((input.value * 0.13));
document.getElementById('invest_output_result-4').innerHTML = out.toFixed(2);
};
</script>
Thank you so much!
Kind regards, Fabian
Based on the user's locale:
new Intl.NumberFormat().format(3500)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#basic_usage
You can use
out.toFixed(2).replace('.', ',');
This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 5 years ago.
i want to get the output "total_amounts" like,ex(255.545645 = 255.54) and (150 =150.00). This is my code
$('.topag').on('input', function(){
var parent = $(this).closest('.calt');
var totalAmt = parseFloat(parent.find('.totalpr').val());
var quantity = parseFloat($(this).val());
parent.find('.total_amount').text(totalAmt*quantity);
$('#total_amounts').val(totalAmt*quantity);
$('#total_amounts').val(total);
calcul_total_quatities();
How can i fix this issue.
var total = 23.64654465;
var total_dec = total.toFixed(2);
document.getElementById("load").innerHTML = total_dec;
<p id="load"></p>
You can use the toFixed() method. The details for this can be found at the following link. See code below for coding needed for your scenario
var totalqty = totalAmt*quantity;
var total = totalqty.toFixed(2);
document.getElementById("total_amounts").innerHTML = total;
This question already has answers here:
Javascript to convert string to number? [duplicate]
(4 answers)
Closed 7 years ago.
I am pretty new in JavaScript and JQuery and I am having a strange behavior with a simple mathematical sum into a JQuery function that retrieve numbers from some input field inside a JSP page (I can't put a JSFiddle because the values are take from jsp model object)
So, this is my JQuery function:
$("#variazioneUlterioreSaldoInput").bind('change keyup', function() {
console.log("VALUE CHANGED !!!");
var ulterioreSaldo = $("#variazioneUlterioreSaldoInput").val(); // Non ha separatori di migliaia o di decimanli quindi non devo eseguire replace
var saldo = $("#saldo").val().replace(/[^0-9,]/g, '').replace(",",".");
var anticipo = $("#anticipo").val().replace(/[^0-9,]/g, '').replace(",",".");
var totalePagamento = ulterioreSaldo + saldo + anticipo;
console.log("ulterioreSaldo: " + ulterioreSaldo );
console.log("ulterioreSaldo type: " + typeof ulterioreSaldo);
console.log("saldo: " + saldo);
console.log("saldo type: " + typeof saldo);
console.log("anticipo: " + anticipo);
console.log("anticipo type: " + typeof anticipo);
console.log("totalePagamento: " + totalePagamento);
console.log("totalePagamento type: " + typeof totalePagamento);
});
that retrieve and sum 3 values obtained from 3 differents input fields.
So for the values retrieved from the input tags having id="saldo" and id="anticipo" I apply a replace with 2 regex because these are strings that represent formatted number so before sum I have to obtain a plain number.
The problem is that when I perform the sum of these values by this line:
var totalePagamento = ulterioreSaldo + saldo + anticipo;
I obtain this wrong outout related to the sum: totalePagamento: 0.010.004499.48, this is the entire log into the FireBug Console.
VALUE CHANGED !!!
ulterioreSaldo: 0.01
ulterioreSaldo type: string
saldo: 0.00
saldo type: string
anticipo: 4499.48
anticipo type: string
totalePagamento: 0.010.004499.48
totalePagamento type: string
As you can see I have also printed the type of the retrieved objects and seems that are String and not Number so when I use the + operator these values are concatenated and not summed.
How can I correctly converts these values into Number and sum it?
Since you are adding decimal numbers try using parseFloat method:
var totalePagamento = parseFloat(ulterioreSaldo) + parseFloat(saldo) + parseFloat(anticipo);
And if you want only 2 digits after decimal use:
totalePagamento.toFixed(2);
use parseFloat() to sum the values
var totalePagamento = parseFloat(ulterioreSaldo) + parseFloat(saldo) + parseFloat(anticipo);
totalePagamento = totalePagamento.toFixed(2); //it will set floating point to 2 values e.g. 40.22
You have to use parseFloat() function to convert string into numbers. The parseFloat() function parses a string argument and returns a floating point number.
$(document).ready(function(){
var ulterioreSaldo = "0.01";
var saldo = "0.0";
var anticipo = "4499.48";
var sum = parseFloat(ulterioreSaldo) + parseFloat(saldo) + parseFloat(anticipo);
alert(sum);
});
Here is a Demo for the same.
This question already has answers here:
How to use split?
(4 answers)
Closed 7 years ago.
I have a string like this 132+456 or 132-456 or 132*456..etc ,it changes dynamically but I need to split this into 132 and 456 how to do it using pure java script?
It should be as easy as:
var parts = '132+456'.split('+');
parts[0]; //132
parts[1]; //456
Like so
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
// document.writeln only for example
document.writeln(a);
document.writeln(b);
Java
String[] values='132+456'.split('+');
String firstPart=value[0]; // has 132
for js
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
This question already has answers here:
Is there a JavaScript function that can pad a string to get to a determined length?
(43 answers)
Closed 8 years ago.
I know this is a gimme, but I'm trying to make the filenames serialized with four digits instead of one. This function is for exporting PNG files from layers within Adobe Illustrator. Let me know if you ever need icons - much respect.
var n = document.layers.length;
hideAllLayers ();
for(var i=n-1, k=0; i>=0; i--, k++)
{
//hideAllLayers();
var layer = document.layers[i];
layer.visible = true;
var file = new File(folder.fsName + '/' +filename+ '-' + k +".png");
document.exportFile(file,ExportType.PNG24,options);
layer.visible = false;
}
Use util.printf (see the Acrobat API, page 720):
var file = new File(util.printf("%s/%s-%04d.png", folder.fsName, filename, k));
You can pad your number to the left and take the last four characters like this:
var i = 9;
var num = ("0000"+i);
var str = "filename"+(num.substring(num.length-4)); //filename0009
Or shorter
str = ("0000" + i).slice(-4)
Thanks to this question