Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to Know how to remove NaN from txtTotalAmount Textbox? Please Help


JavaScript
function CalculateTotal(objCtrl) 
{
var txtamount = parseFloat(document.getElementById('<%=txtInvoiceAmount.ClientID%>').value);
var txtother = parseFloat(document.getElementById('<%=txtOtherCharges.ClientID%>').value);
 
var total = parseFloat(txtamount+ txtother);
document.getElementById('<%=txtTotalAmount.ClientID%>').value = total;

}
Posted
Updated 4-Sep-14 20:22pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Sep-14 2:25am    
Please, be more precise. Do you see some double of float value NaN (say, in the debugger), or do you see the string "NaN" somewhere.
Do you know this value, double.NaN (not a number) and its meaning? It can come in calculations or directly assigned to a double (float) variable/member.
—SA

Use the isNaN[^] function, namely:
JavaScript
if (isNaN(total)) total = 0;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Sep-14 2:32am    
5ed, but OP's problem is more of a problem of understanding.
I tried to explain the matter in Solution 2.
—SA
amnk.info 5-Sep-14 3:14am    
Thanks CPallini it works (Y)
CPallini 5-Sep-14 3:34am    
You are welcome.
Please see my comment to the question.

NaN (not a number) is one of the legitimate values added to the domain of the floating-point type, to denote "not a number" result of some calculations, such as division 0.0/0.0 (it does not cause exception but returns NaN). This is the part of the IEEE-754 standard. Please see:
http://en.wikipedia.org/wiki/Floating_point[^],
http://en.wikipedia.org/wiki/IEEE_754[^].

The question is not 100% clear, but one source of such value is the Javascropt call parseFloat(string). It will return NaN is the string cannot be interpreted in a numeric format. Please see: http://www.w3schools.com/jsref/jsref_parsefloat.asp[^].

—SA
 
Share this answer
 
v2
Comments
CPallini 5-Sep-14 3:34am    
5. 'The other side of the question' :-)
Sergey Alexandrovich Kryukov 5-Sep-14 9:41am    
Thank you, Carlo.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900