Solved

how to sum two numbers in c# page using javascript

Posted on 2010-11-19
9
480 Views
Last Modified: 2012-05-10

i want to add two numbers that are generated dynamically which contain symbols like dollar and comma.

ex:
 [txtField1] + [txtField2] =  [txtsum]
$1,111.11 + $1,111.11 = 2222.22

function calsum()
{
   var objField1 = document.getElementByUD('txtField1');
   var objField2 = document.getElementByUD('txtField2');

  var sum1 = parseFloat(objField1.value) + parseFloat(objField2.value);

   document.getElementByUD('txtsum')  = sum1.value;

}
0
Comment
Question by:Bharat Guru
9 Comments
 
LVL 3

Expert Comment

by:brd24gor
ID: 34175078
You can use String.Replace to filter out commas and dollar signs, do the float conversion, then add them up.

txtField1.Replace("$","");
txtField2.Replace(",","");
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34175089
In javascript, you would use:

var sum1 = parseFloat(objField1.value.replace(/\$|\,/g,'')) + parseFloat(objField2.value.replace(/\$|\,/g,''));
0
 

Author Comment

by:Bharat Guru
ID: 34175663
when txtField1 or txtField2 is null then result is shown as NaN.  
when field1 or field2 are null then result should be ''  (empty)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34176016
Try this out:
function calsum()
{
	var objField1 = document.getElementById('txtField1').value.replace(/[^0-9.]/g, '');
	var objField2 = document.getElementById('txtField2').value.replace(/[^0-9.]/g, '');

	var sum1 = (objField1 == null ? 0 : parseFloat(objField1)) + (objField2 == null ? 0 : parseFloat(objField2));

	document.getElementById('txtsum').value = sum1;
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34176114
This would be a bit safer, I think:
function calsum()
{
	var objField1 = document.getElementById('txtField1').value.replace(/[^0-9.]|\.(?=\d*\.)/g, '');
	var objField2 = document.getElementById('txtField2').value.replace(/[^0-9.]|\.(?=\d*\.)/g, '');

	var sum1 = (objField1 == null ? 0 : parseFloat(objField1)) + (objField2 == null ? 0 : parseFloat(objField2));

	document.getElementById('txtsum').value = sum1;
}

Open in new window

0
 

Author Comment

by:Bharat Guru
ID: 34176387
minus addition doesn't work
 5   -5   = shows 10 instead of 0
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 125 total points
ID: 34176565
Silly me...


Corrected:
function calsum()
{
	var objField1 = document.getElementById('txtField1').value.replace(/[^0-9.-]|(\d)-|\.(?=\d*\.)/g, '$1');
	var objField2 = document.getElementById('txtField2').value.replace(/[^0-9.-]|(\d)-|\.(?=\d*\.)/g, '$1');

	var sum1 = (objField1 == null ? 0 : parseFloat(objField1)) + (objField2 == null ? 0 : parseFloat(objField2));

	document.getElementById('txtsum').value = sum1;
}

Open in new window

0
 

Author Closing Comment

by:Bharat Guru
ID: 34176663
Thanks a lot perfect
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34176681
NP. Glad to help  :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question