Wrox Programmer Forums
|
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 29th, 2012, 07:55 PM
Authorized User
 
Join Date: Oct 2012
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Smile Displaying actual figures rather than exponentials

Hi,

I am customising the converter in lesson 11, exercise 6 and have a question.

Layout
  • I have a text box to enter the number to convert (tbxLengthToConvert)
  • Radio button to select centimetres unit (rbCm)
  • text boxes for the converted types mm, cm, metres, kilometres, inches and feet.
  • Convert button (btnLengthConvert)

Functionality
When i enter an amount in the conversion text box (tbxLengthToConvert), click the centimetres radio button (rbCm) and click the Convert button (btnLengthConvert) it converts the amount entered using the details in the code. Thats all good.

The Problem
The problem i have is in the way it displays.
In my code for converting cm to km, i am multiplying tbxLengthToConvert by 0.00001. For the feet conversion, it multiplies by 0.032808399.

When i test by typing 1 in the tbxLengthToConvert text box, the feet conversion is displayed as 0.032808399 but kilometres is displayed as 1E-05.

I understand that this is probably due to the answer in centimetres being 0.00001 but is it possible to make it display 0.00001 instead of 1E-05 which is difficult to understand?

Code:
private void btnLengthConvert_Click(object sender, EventArgs e)
        {
            //CENTIMETRES
            if (rbCM.Checked == true)
            {
                double length = double.Parse(tbxLengthToConvert.Text);
                double mm = length * 10;
                double cm = length * 1;
                double mt = length * 0.01;
                double km = length * 0.00001;
                double inches = length * 2.54;
                double feet = length * 0.032808399;

                tbxLenMm.Text = mm.ToString();
                tbxLenCm.Text = cm.ToString();
                tbxLenMetres.Text = mt.ToString();
                tbxLenKm.Text = km.ToString();
                tbxLenIn.Text = inches.ToString();
                tbxLenFt.Text = feet.ToString();

            }

        }
__________________
"These pretzels are making me thirsty"
 
Old October 30th, 2012, 10:22 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

No problem.

The program can display values in many formats. The E (or e) format gives you an exponent as in 1E-05. The F (or f) format gives you fixed point as in 0.000001. My guess is that by default it's using the G (or g) format, which gives you E or F, whichever produces a smaller result.

To pick one, just add the format specifier you want as a string parameter to the ToString method. In this case, you want:

Code:
tbxLenKm.Text = km.ToString("F");
That should do it. For more information on formatting strings, see:
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old October 30th, 2012, 06:57 PM
Authorized User
 
Join Date: Oct 2012
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default It helps but...

I tried all different combos and can get the desired result when converting 1cm to km 0.00001.

I used
Code:
tbxLenKm.Text = km.ToString("F5");
The calculator shows the answer correctly. However, when i convert 1000cm for instance, the answer is 0.01000 or converting 1,000,000cm, the answer is 10.00000.

I dont really want the extra zeros after, i just want to be able to show preceeding zeros for very small answers (like in 0.00001 not 1.00000)

-----Edit-----
I have done further testing not using a format and had some strange results.

converting inches to kilometres. Using the code below: i get the answer 2.54E-05 when i run the program and convert the number 1.
Code:
double km = length * 0.0000254;
tbxLenKm.Text = km.ToString();
converting inches to feet. Using the code below: i get the answer 0.083333333333 when i run the program and convert the number 1.
Code:
double feet = length * 0.083333333333;
tbxLenFt.Text = feet.ToString();
Why is one giving an exponent and the other doesnt? I also changed the km conversion rate to have the same length number (0.000025400000) and it still gave an exponent.

Weird.
__________________
"These pretzels are making me thirsty"

Last edited by MinusZero; October 30th, 2012 at 08:13 PM.. Reason: Added information
 
Old October 30th, 2012, 09:33 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Code:
tbxLenKm.Text = km.ToString("F5");
I think that should produce fixed format (no exponent) with 5 digits of precision. Try:

Code:
tbxLenKm.Text = km.ToString("F");
Quote:
Why is one giving an exponent and the other doesnt?
My guess is that it thinks it cannot use an exponent for 0.083333333333 without losing precision. Again I think it's using the G format and going with what's shorter. In this case I think it would try to display with an exponent as 0.83333333333E-002.

Quote:
I also changed the km conversion rate to have the same length number (0.000025400000) and it still gave an exponent.
I think adding the 0s at the end doesn't fool it. Try something like 0.000025400001 and see what it does.

In general, though, it sounds like you might just want to use the F format as in:

Code:
tbxLenKm.Text = km.ToString("F");
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old October 31st, 2012, 11:46 PM
Authorized User
 
Join Date: Oct 2012
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Using F gives the answer 0.00. I guess i might just have to leave it and hope i dont need to convert 1 cm to kilometres hehe
__________________
"These pretzels are making me thirsty"
 
Old November 1st, 2012, 11:01 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hmm... I suppose it's sort of like it's saying the value is 0 within reasonable limits of precision. In a way that's true.

I think if you're of a scientific bent and might want to express 1 cm in kilometers, you'd probably use scientific notation anyway.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Numerals into figures renu2214 Classic ASP Professional 0 April 17th, 2007 09:34 AM
Comparing the figures in two columns on each sheet sibo32000 Excel VBA 3 February 26th, 2007 01:29 PM
Linking via Actual Address SerranoG Access 2 July 16th, 2006 02:33 AM
XML/XSL to actual HTML tcstom XML 3 December 9th, 2005 01:36 PM
searching through a sheeft for minus figures Mark_Harrison Excel VBA 2 September 11th, 2003 03:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.