C# Decimal Issue

Hi,

When running this code, I want the distance in list box to be a decimal.

  decimal MPH;
                decimal distance;
                int hours;
                int count = 1;
                if (decimal.TryParse(mphTextBox.Text, out MPH))
                {
                    if (int.TryParse(hoursTextBox.Text, out hours))
                        while (count <= hours)
                        {
                            distance = (MPH * hours);
                            distanceListBox.Items.Add("After Hour " + count + " the distance is " + [b]distance.ToString("#.#")[/b]);
                            count++;
                        }
                }

Open in new window


Where did I go wrong so I can correct it?

It shows up as 60 not 60.0 which is what I want. I am open to re-working some of the code.

Thanks!
LVL 3
Computer GuyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
This will force a zero (or other) after the decimal point:
distance.ToString("#.0")
Computer GuyAuthor Commented:
What happens if the distance was 60.5. I want to code it so it would run proper not just end in a zero.
AndyAinscowFreelance programmer / ConsultantCommented:
Then you get 60.5.  As I said it forces a digit (zero or other) after the decimal point.
Fernando SotoRetiredCommented:
Hi audiotech520;

Change this line of code with the "#.#" format specifier to this "#.0" for the results you wish.

distanceListBox.Items.Add("After Hour " + count + " the distance is " + [b]distance.ToString("#.#")[/b]);

// To this line

distanceListBox.Items.Add("After Hour " + count + " the distance is " + [b]distance.ToString("#.0")[/b]);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
Out of interest what was did you not understand from my comment telling you exactly the same thing?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.