473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

adding


I am occasionally having problems with adding numbers together. Sometimes
(not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the
following method to overcome this problem, however I feel it is a bit
contrived, and i am sure there is a proper way of doing it!

var asd = 2
var zxc=2
asd=asd-(-1*zxc)
Jul 20 '05 #1
4 2034
"Stuart" <kf****@yytd.gft> writes:
I am occasionally having problems with adding numbers together. Sometimes
(not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the
following method to overcome this problem, however I feel it is a bit
contrived, and i am sure there is a proper way of doing it!


<URL:http://jibbering.com/faq/#FAQ4_21>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
On Wed, 5 Nov 2003, Stuart wrote:
I am occasionally having problems with adding numbers together. Sometimes
(not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the
following method to overcome this problem, however I feel it is a bit
contrived, and i am sure there is a proper way of doing it!


If I want to force conversion, and I'm lazy, I go:

baz = 0 + foo + bar;

It's the same ideas as when you want to turn stuff into a string:

baz = "" + foo + bar;

But in general, 2 + 2 should /never/ give you 22. Unless you're pulling
the numbers from, say a input box (in which case you're pulling text!)

Regards,
Svend
Jul 20 '05 #3
"Svend Ezaki Tofte (DIKU)" <di******@diku.dk> writes:
If I want to force conversion, and I'm lazy, I go:

baz = 0 + foo + bar;
That would be *too* lazy.

var foo="2", bar="2";
alert(0 + foo + bar);

This alerts "022". The type concversion favors strings over numbers,
so when you add a string to a number, the number is converted to a
string, not the other way around. The order doesn't matter.

Just do as the FAQ says :) The fastest method is the unary prefix
plus.
var baz = +foo + +bar;
<URL:http://jibbering.com/faq/#FAQ4_21>
It's the same ideas as when you want to turn stuff into a string:

baz = "" + foo + bar;


Which works, because the string wins, and evaluation is left to right.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
JRS: In article <Pi*******************************@brok.diku.dk> , seen
in news:comp.lang.javascript, Svend Ezaki Tofte (DIKU)
<di******@diku.dk> posted at Thu, 6 Nov 2003 06:29:23 :-
On Wed, 5 Nov 2003, Stuart wrote:
I am occasionally having problems with adding numbers together. Sometimes
(not always) 2 + 2 = 22, Obviousy this is a string issue. I am using the
following method to overcome this problem, however I feel it is a bit
contrived, and i am sure there is a proper way of doing it!


If I want to force conversion, and I'm lazy, I go:

baz = 0 + foo + bar;


That's a nice easy method; the only fault, as far as I can see, is that
it does not work.

Tested solutions are safer.

Read the FAQ.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1...
10
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
1
by: hzgt9b | last post by:
When adding my VB .NET solution (with two sub-projects) the folder structure in VSS gets an extra level of folders... For example, here's the structure of the solution on my C:\...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
0
by: Sileesh | last post by:
Hi I have html table and a Button in an Aspx page. I am adding one row with some textboxes to Html table each time i click on the Button thru Javascript. Now problem is when when i try to...
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
6
by: vb | last post by:
Hi, I am new to .Net. I am using a Combo Box in my windows forms. I am adding the items by creating the instances and adding the same to the list. My questions/doubts are: 1. If I have 25 to...
9
by: Kadett | last post by:
Hi all, I have following problem: I'm creating a ListView (Details) control at run-time and filling it with some records (let's say 10 000). This operation seems to be quite fast, but when I call...
0
by: AndyL69 | last post by:
Hello I've got a very strange Problem. When im adding a new ACE entry to a UNC Direcotry the inherented ACL's will be lost. When I'm adding a new ACE to a directory / file in this UNC path the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.