Solved

how to sum two numbers in c# page using javascript

Posted on 2010-11-19
9
476 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
 
LVL 74

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 74

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 74

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 74

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
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)

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now