floating values issue
-
Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared:
float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126;
on which I do the following calcluation:overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString());
The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R -
Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared:
float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126;
on which I do the following calcluation:overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString());
The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R -
Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared:
float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126;
on which I do the following calcluation:overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString());
The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. R -
float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126; overallTotalAmps = (float)overTotalWatt / (float)voltage;
led mike
Great stuff!! Thanks to both of you that replied. It works perfectly!!! :-D
-
Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared:
float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126;
on which I do the following calcluation:overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString());
The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. RHi, adding to the other replies: many problem domains are continuous by nature; if so, you should not use integers at all. The simplest approach is to make overallTotalAmps , voltage, overTotalWatt, ... all float or double. That way you don't have to cast, and won't have any rounding errors that may go unnoticed. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared:
float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126;
on which I do the following calcluation:overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString());
The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. RI know you've already got an answer, but I was a bit baffled by the use of ToString and float.parse so I thought I'd give you a rundown of what that actually does overallTotalWatt / voltage => (int)126 / (int)230 => 0 (since we're dividing ints) 0.ToString() => "0" float.Parse("0") => 0.00
-- Help me! I'm turning into a grapefruit! Buzzwords!
-
Hi, I havn't dealt much with floating numbers / calculation since recently. Let me explain my issue: I have the following 3 values declared:
float overallTotalAmps = 0; int voltage = 230; int overTotalWatt = 126;
on which I do the following calcluation:overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString());
The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230). Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here? :wtf: Thanks. RAnyone that ToString's a number to parse should be dragged outside and shot.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway