Click here to Skip to main content
16,016,087 members
Home / Discussions / C#
   

C#

 
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
The Nightcoder28-May-08 22:52
The Nightcoder28-May-08 22:52 
GeneralRe: float calculation issue Pin
George_George31-May-08 3:15
George_George31-May-08 3:15 
Thanks Peter,


Your reply is really great! I posted my code below again. My confusion is, why the ToString method will do the rounding work? I think it should be the float number calculation which will do the rounding work.

In my sample, you can see in the float calculation,

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

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


there is no rounding in Result1 and Result2.

But in the ToString method,

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


There is rounding for Result2String. Any ideas?

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

            if ((ResultTotalReceived - ResultTotalSent) < float.Epsilon)
            {
                // not executed here
                Console.WriteLine("Treated the same. ");
            }
        }
    }
}



regards,
George
GeneralRe: float calculation issue Pin
The Nightcoder2-Jun-08 2:35
The Nightcoder2-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
The Nightcoder3-Jun-08 2:39
The Nightcoder3-Jun-08 2:39 
GeneralRe: float calculation issue Pin
George_George3-Jun-08 2:51
George_George3-Jun-08 2:51 
GeneralRe: float calculation issue Pin
The Nightcoder3-Jun-08 3:18
The Nightcoder3-Jun-08 3:18 
GeneralRe: float calculation issue Pin
George_George3-Jun-08 21:57
George_George3-Jun-08 21:57 
GeneralRe: float calculation issue Pin
The Nightcoder4-Jun-08 2:55
The Nightcoder4-Jun-08 2:55 
GeneralRe: float calculation issue Pin
George_George4-Jun-08 22:31
George_George4-Jun-08 22:31 
GeneralRe: float calculation issue Pin
The Nightcoder4-Jun-08 23:08
The Nightcoder4-Jun-08 23:08 
GeneralRe: float calculation issue Pin
George_George4-Jun-08 23:28
George_George4-Jun-08 23:28 
AnswerRe: float calculation issue (decimal is no solution) [modified] Pin
The Nightcoder28-May-08 23:12
The Nightcoder28-May-08 23:12 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George31-May-08 3:28
George_George31-May-08 3:28 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder2-Jun-08 3:02
The Nightcoder2-Jun-08 3:02 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George3-Jun-08 2:09
George_George3-Jun-08 2:09 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder3-Jun-08 2:48
The Nightcoder3-Jun-08 2:48 
GeneralRe: float calculation issue (decimal is no solution) Pin
George_George3-Jun-08 3:00
George_George3-Jun-08 3:00 
GeneralRe: float calculation issue (decimal is no solution) Pin
The Nightcoder3-Jun-08 3:37
The Nightcoder3-Jun-08 3:37 

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.