Solved

how to sum two numbers in c# page using javascript

Posted on 2010-11-19
9
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 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