Click here to Skip to main content
15,899,937 members
Home / Discussions / C#
   

C#

 
GeneralRe: Chat who is online box Pin
Bobbz26-May-08 4:05
Bobbz26-May-08 4:05 
QuestionIn Excel worskheet how to find out that a particulat cell is selected or not ? Pin
Varad_Rajan26-May-08 1:00
Varad_Rajan26-May-08 1:00 
GeneralRe: In Excel worskheet how to find out that a particulat cell is selected or not ? Pin
Christian Wikander26-May-08 2:05
Christian Wikander26-May-08 2:05 
GeneralRe: In Excel worskheet how to find out that a particulat cell is selected or not ? Pin
Varad_Rajan26-May-08 2:24
Varad_Rajan26-May-08 2:24 
QuestionDatabase Suspect proble : please Help-Urgent Pin
rockz...26-May-08 0:50
rockz...26-May-08 0:50 
GeneralRe: Database Suspect proble : please Help-Urgent Pin
Christian Wikander26-May-08 2:04
Christian Wikander26-May-08 2:04 
Questionreg:displaying each row as each column in gridview contol Pin
Member 400849226-May-08 0:21
Member 400849226-May-08 0:21 
Questionfloat calculation issue Pin
George_George26-May-08 0:21
George_George26-May-08 0:21 
Hello everyone,


Here is my simple bonus assignment program. The issue I met with is, the ResultTotalReceived is larger then ResultTotalSent, which violates corporation policy and exception is thrown.

The program works in this way,

1. At source side, calculate and assign the bonus according to each worker's factor (100F for worker1 and 300F for worker2 in my sample). All figures are float type.
2. Convert the float type to string and sent to another destination to store the bonus into storage;
3. The detination side will perform basic checking rules before storing the data, e.g. the total bonus assigned should not exceed the total bonus available at source side.

In my code below,

The value of ResultTotalSent is 199.321, and the value of ResultTotalReceived is 199.321045, which is larger than ResultTotalSent.

My questions is,

1. If I want to solve this issue at source side, what is the elegant way to solve this issue? Currently, my temp solution is using ToString("F2"). Any issues with this solution?

2. Why there is such issue? It is the issue of ToString of Float class -- I have this suspecision is because ResultTotalSent is precise but after ToString conversion the result and conversion back at detination side, it is not precise?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestFloat
{
    class Program
    {
        static void Main(string[] args)
        {
            float TotalBonus = 199.321F;
            float Worker1 = 100F;
            float Worker2 = 300F;

            float Result1 = TotalBonus * Worker1 / (Worker1 + Worker2);

            float Result2 = TotalBonus * Worker2 / (Worker1 + Worker2);

            float ResultTotalSent = Result1 + Result2;

            
            string Result1String = Result1.ToString();
            string Result2String = Result2.ToString();

            // sending to another computer using string

            // received from another computer using string
            string ReceivedString1 = Result1String;
            string ReceivedString2 = Result2String;
            float Received1 = float.Parse(ReceivedString1);
            float Received2 = float.Parse(ReceivedString2);
            float ResultTotalReceived = Received1 + Received2;

            // sanity checking failed, since ResultTotalReceived > TotalBonus

            return;
        }
    }
}



thanks in advance,
George
AnswerRe: float calculation issue Pin
GuyThiebaut26-May-08 0:48
professionalGuyThiebaut26-May-08 0:48 
GeneralRe: float calculation issue Pin
George_George26-May-08 15:15
George_George26-May-08 15:15 
GeneralRe: float calculation issue Pin
GuyThiebaut26-May-08 22:28
professionalGuyThiebaut26-May-08 22:28 
GeneralRe: float calculation issue Pin
George_George27-May-08 3:13
George_George27-May-08 3:13 
GeneralRe: float calculation issue Pin
GuyThiebaut27-May-08 4:36
professionalGuyThiebaut27-May-08 4:36 
GeneralRe: float calculation issue Pin
George_George28-May-08 0:23
George_George28-May-08 0:23 
GeneralRe: float calculation issue Pin
DaveyM6927-May-08 12:05
professionalDaveyM6927-May-08 12:05 
GeneralRe: float calculation issue Pin
George_George28-May-08 0:26
George_George28-May-08 0:26 
GeneralRe: float calculation issue Pin
DaveyM6928-May-08 1:42
professionalDaveyM6928-May-08 1:42 
GeneralRe: float calculation issue Pin
George_George28-May-08 18:54
George_George28-May-08 18:54 
AnswerRe: float calculation issue Pin
DaveyM6926-May-08 1:40
professionalDaveyM6926-May-08 1:40 
GeneralRe: float calculation issue Pin
George_George26-May-08 15:16
George_George26-May-08 15:16 
AnswerRe: float calculation issue Pin
Peter Josefsson Sweden28-May-08 22:52
Peter Josefsson Sweden28-May-08 22:52 
GeneralRe: float calculation issue Pin
George_George31-May-08 3:15
George_George31-May-08 3:15 
GeneralRe: float calculation issue Pin
Peter Josefsson Sweden2-Jun-08 2:35
Peter Josefsson Sweden2-Jun-08 2:35 
GeneralRe: float calculation issue Pin
George_George3-Jun-08 1:59
George_George3-Jun-08 1:59 
GeneralRe: float calculation issue Pin
Peter Josefsson Sweden3-Jun-08 2:39
Peter Josefsson Sweden3-Jun-08 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.