473,386 Members | 1,720 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,386 software developers and data experts.

C# typecast Int to Double

109 100+
This might be an eazy problem to fix,
assume all works except the int to double
Expand|Select|Wrap|Line Numbers
  1. double total = 0;
  2. double Qty[10];
  3.  
  4. int price[10];
  5.  
  6. for(int i = 0; i < 9; i++)
  7. {
  8.  total += Qty[i] * (double)price[i];
  9. }
  10.  
  11.  
i need to get total in the forme of a double.
any ideas?
Apr 28 '08 #1
6 20182
Plater
7,872 Expert 4TB
EDIT:
Actually, I think your example might be a little off. Wouldn't you want price to be a double and quantity to be the int? Your array declarations seemed to be off as well, since those aren't valid declarations.

I just did this, without typecasting at all and total stayed a double:
Expand|Select|Wrap|Line Numbers
  1. double total = 0;
  2.             double[] Qty = new double[10];
  3.             Qty[0] = 1.1;
  4.             Qty[1] = 4.5;
  5.             Qty[2] = 9.7;
  6.             Qty[3] = 2.6;
  7.             Qty[4] = 0.7;
  8.  
  9.             int[] price = new int[10];
  10.             price[0] = 1;
  11.             price[1] = 4;
  12.             price[2] = 3;
  13.             price[3] = 7;
  14.  
  15.             for (int i = 0; i < 9; i++)
  16.             {
  17.                 total += Qty[i] * price[i];
  18.             }
  19.  
Total was a double just fine. So I am thinking in your ACTUAL code there is something else wrong?
Apr 28 '08 #2
SpecialKay
109 100+
here is the Accualy code

Expand|Select|Wrap|Line Numbers
  1.             infos.TotalCost += (double)(infos.Order.OrderLineItemCollection[i - 1].Quantity * double.Parse(infos.Order.OrderLineItemCollection[i - 1].Price));
  2.  
  3.  
broken down

Expand|Select|Wrap|Line Numbers
  1. infos.TotalCost = Double
  2. infos.Order.OrderLineItemCollection[i - 1].Quantity -> Returns int
  3. infos.Order.OrderLineItemCollection[i - 1].Price -> returns String (double.Parse works for this)
  4.  
  5.  
Apr 28 '08 #3
Plater
7,872 Expert 4TB
Your actual code looks like it should work fine?
Apr 28 '08 #4
SpecialKay
109 100+
Ya i agree, there must be a problem somewhere else.
im going to snoop around and see what i come up with.
In other news:

if you multiply an int (2) by a double (1.3) into a double like

Expand|Select|Wrap|Line Numbers
  1. double ans = 0;
  2. int a = 2;
  3. double b = 1.3;
  4.  
  5. ans = a*b;
  6.  
  7. does ans become an int?
  8. what would the value of ans be?
  9.  
Apr 28 '08 #5
Plater
7,872 Expert 4TB
It should be a double.
You can run all these testcases yourself to see the answer quickly though.
Apr 28 '08 #6
SpecialKay
109 100+
you are write :)
thanks tho, i did after i posted the question, so i really didnt need to post it. but o well.
Apr 28 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Venkat | last post by:
Hi All, I want to typecast int to std::string how can i do it. Here is the sample code. int NewList; //Fill the NewList with integers values. .......
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
8
by: Trishia Rose | last post by:
this is something ive always wondered, does it take cpu time at run time to typecast or just at compile time? for example consider the two little bits of code: int a = 5; int b = a; and: ...
10
by: Peter Drese | last post by:
Hello, I have to write an exam and I am not sure about implicit typecasting. Can somebody please tell me of what type the following c-statements are? And why they are of this type? It is also...
4
by: Michael Mair | last post by:
Hi there, actually, I have posted the same question in g.g.help. As there were no answers, I am still not sure whether this is a bug or only something open to the compiler that is seemingly...
1
by: nsj | last post by:
How to typecast double into ascii or string? Please help me.
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
1
by: niharnayak2003 | last post by:
Hi All, I am facing the problem in Typecast inside the Generic class. I need a function which will take two param 1:-Xpath 2:- XMLDOC and return me what i will need. here is the code for...
6
by: fbooyens | last post by:
I'm new to C# and come from an embedded C background so I hope I'm not embarassing myself not too much. I need to write a simulator for a variable frequency drive which sends device parameters to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.