Calgarypuck Forums - The Unofficial Calgary Flames Fan Community

Go Back   Calgarypuck Forums - The Unofficial Calgary Flames Fan Community > Main Forums > The Off Topic Forum > Tech Talk
Register Forum Rules FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread
Old 05-26-2010, 09:03 PM   #1
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default Truncating Numbers Issue in C#?

This is driving me nuts... I'm trying to complete a very simple assignment, but I can't seem to get past this issue I'm having with decimals getting chopped off.

Here's my code. I've pasted the actual assignment itself and commented it out, so you can see what I'm trying to do. Keep in mind this is a pretty basic assignment, and it's just a quick console app, so there is no error-handling or crash-proofing.

Code:
static void Main(string[] args)
{
    //Use random numbers to simulate the repeated tossing of a fair coin. 
    //Letting the user input the number of tosses, report the percentage of outcomes that are heads. 
    //Allow the user to repeat the calculation as often as desired.

    string again;
    Random rnd = new Random();

    do
    {
        int heads = 0;
        int tails = 0;
        int numOfTosses = 0;

        Console.Write("How many tosses would you like to make? ");
        numOfTosses = int.Parse(Console.ReadLine());

        int tossCount = numOfTosses;
        while (tossCount > 0)
        {
            int toss = rnd.Next(1, 3);
            if (toss == 1) 
                heads++;
            else 
                tails++;
            tossCount--;
        }

        Console.WriteLine("Number of Heads: {0} ({1}%).", heads, (heads / numOfTosses) * 100);
        Console.WriteLine("Number of Tails: {0} ({1}%).", tails, (tails / numOfTosses) * 100);
        Console.WriteLine("");

        Console.Write("Toss again? (yes/no) ");
        again = Console.ReadLine();

        Console.WriteLine("");
    }
    while (again == "yes");
}
The problem is that I'm trying to get the following bit:

Code:
Console.WriteLine("Number of Heads: {0} ({1}%).", heads, (heads / numOfTosses) * 100);
Console.WriteLine("Number of Tails: {0} ({1}%).", tails, (tails / numOfTosses) * 100);
To show a proper %, but it keeps truncating the decimal and then multiplying by zero. So, instead of (16 / 25) * 100 returning 64, it's just returning 0 (because it's taking 16/25=0.64 and truncating the .64, leaving only a 0... and then multiplying it by 100).

I know it has something to do with type (int, double, float, etc). I can get it to work perfectly, if I change everything from int to double, but there has to be a way to leave my int types alone and still get this number to print properly...

As a workaround, I'm using {(heads * 100) / numOfTosses}, but even that isn't working as it's still truncating the decimal in the calc and returning two % that don't add up to 100 (like 73% and 26%).

Last edited by FanIn80; 05-26-2010 at 10:09 PM.
FanIn80 is offline   Reply With Quote
Old 05-26-2010, 09:39 PM   #2
Rathji
Franchise Player
 
Rathji's Avatar
 
Join Date: Nov 2006
Location: Supporting Urban Sprawl
Exp:
Default

I haven't done much in C# but there is probably no way to get an int to hold a decimal value.

Go with a float.
__________________
"Wake up, Luigi! The only time plumbers sleep on the job is when we're working by the hour."
Rathji is offline   Reply With Quote
The Following User Says Thank You to Rathji For This Useful Post:
Old 05-26-2010, 09:42 PM   #3
Fire
Franchise Player
 
Fire's Avatar
 
Join Date: Oct 2001
Location: Calgary, AB
Exp:
Default

Try casting to a double:

Console.WriteLine("Number of Heads: {0} ({1}%).", heads, (double) (heads / numOfTosses) * 100);
Console.WriteLine("Number of Tails: {0} ({1}%).", tails, (double) (tails / numOfTosses) * 100);
__________________

Fire is offline   Reply With Quote
The Following User Says Thank You to Fire For This Useful Post:
Old 05-26-2010, 09:43 PM   #4
sureLoss
Some kinda newsbreaker!
 
sureLoss's Avatar
 
Join Date: May 2004
Location: Learning Phaneufs skating style
Exp:
Default

Its an issue of integer division.

when you divide an integer by an integer it will return an integer. For example 25/100 will return 0 but 25/100.0 will return 0.25 in most programming languages.

You need to cast the divisor to a float.

Console.WriteLine("Number of Heads: {0} ({1}%).", heads, (heads / (double) numOfTosses) * 100.0);

edit this will work also:
{(heads * 100.0) / numOfTosses},

Last edited by sureLoss; 05-26-2010 at 09:57 PM.
sureLoss is offline   Reply With Quote
The Following User Says Thank You to sureLoss For This Useful Post:
Old 05-26-2010, 09:48 PM   #5
GGG
Franchise Player
 
GGG's Avatar
 
Join Date: Aug 2008
Location: California
Exp:
Default

An int by definition will never carry a decimal place. It is a whole number.

So if you are doing any division where you will get decimals you need to be using floats. I haven't programed anything in years but I think their is a comand something like percision or truncate that will allow you to set the number of characters displayed when printing a float to the screen.
GGG is offline   Reply With Quote
The Following User Says Thank You to GGG For This Useful Post:
Old 05-26-2010, 10:03 PM   #6
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

Oh man. You guys are life savers! I've been banging my head on this all night. I saw something about casting during a Google search, but it wasn't really clear. I had the syntax all wrong on it, so I scrapped it. I've been messing around with trying to format the number, etc... hell I even spent 30 mins trying to convert everything to a concatenated string!

Whew. That "cast as a different type" thing is exactly what I wanted to do. I just had to round the resulting number, and voila!

Console.WriteLine("Number of Heads: {0} ({1}%).", heads, Math.Round( (heads / (double) numOfTosses) * 100, 2) );
Console.WriteLine("Number of Tails: {0} ({1}%).", tails, Math.Round( (tails / (double) numOfTosses) * 100, 2) );

Thanks again, everyone!

Edit: Though I should probably filter that math through a couple variables so it's easier to read...

double headCount = (heads / (double) numOfTosses) * 100;
double headPercent = Math.Round(headCount, 2);
double tailCount = (tails / (double) numOfTosses) * 100;
double tailPercent = Math.Round(tailCount, 2);

Console.WriteLine("Number of Heads: {0} ({1}%).", heads, headPercent);
Console.WriteLine("Number of Tails: {0} ({1}%).", tails, tailPercent);
Console.WriteLine("");

Last edited by FanIn80; 05-26-2010 at 10:35 PM.
FanIn80 is offline   Reply With Quote
Old 05-27-2010, 01:10 PM   #7
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

Haha.. integer division.. n00b!
__________________
Who is in charge of this product and why haven't they been fired yet?
kermitology is offline   Reply With Quote
The Following 2 Users Say Thank You to kermitology For This Useful Post:
Old 05-27-2010, 01:23 PM   #8
Hack&Lube
Atomic Nerd
 
Join Date: Jul 2004
Location: Calgary
Exp:
Default

Floating point for the win. I should get back into C++. I was doing stuff like this when I was 12 years old. I made a Tic Tac Toe game in Turbo Pascal when I was 10. Then I decided it was boring and I forgot all about programming because I hated math and a lot of the higher level stuff comes a lot easier if you have a good understanding of basic math concepts.
Hack&Lube is offline   Reply With Quote
Old 05-27-2010, 02:04 PM   #9
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

What's funny is that just by reading the title, I knew that you were doing an integer divide.
__________________
Who is in charge of this product and why haven't they been fired yet?
kermitology is offline   Reply With Quote
Old 05-27-2010, 02:05 PM   #10
FanIn80
GOAT!
 
FanIn80's Avatar
 
Join Date: Jun 2006
Exp:
Default

Quote:
Originally Posted by kermitology View Post
What's funny is that just by reading the title, I knew that you were doing an integer divide.
Haha yeah. I knew this was going to be something incredibly basic. I also knew it had to be type-related too, I just couldn't figure out how to convert the part I needed without switching all my integers over.
FanIn80 is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:10 PM.

Calgary Flames
2024-25




Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright Calgarypuck 2021 | See Our Privacy Policy