CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2007
    Posts
    44

    Simple Casting problem

    hi, i cannot find the answer of this in the net.

    string a = "4.00";
    double d = Convert.ToDouble(a);
    double d1 = d * 3;
    MessageBox.Show(d1.ToString());

    it displays "1200" but i want it to display 12.0 or 12.

    How can i convert the string "4.00" to double 4.00 because it converts the string "4.00" to double 400.

    thanks in advance

  2. #2
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: Simple Casting problem

    A good way to convert strings to numbers is the following:
    Code:
    string input = "4.00";
    double value;
    if (Double.TryParse(input, out value))
    {
        // The string was successfully parsed and we have the resulting double in the "value" variable.
    }
    else
    {
        // Failed to parse the string. Set a default value or report an error to the user.
    }
    The out keyword means that the function we're sending the variable to will alter the value of the variable. It's kind of like the ref keyword, except the function must set the variable, and a value doesn't have to be assigned to it when passing it to the function.

  3. #3
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Simple Casting problem

    At first, use code tags while posting here.

    Now the solution. You have to use a format provider. Change your convert statement to:
    Code:
    NumberFormatInfo provider = new NumberFormatInfo( );
    
    string a = "4.00";
    double d = Convert.ToDouble(a, provider);
    Useful or not? Rate my posting. Thanks.

  4. #4
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Simple Casting problem

    Convert.ToDouble says it actually uses Double.Parse. The help for Double.Parse includes these interesting excerpts partially describing the input parameter:
    A series of digits specifying the integral part of the number. Runs of integral-digits can be partitioned by a group-separator symbol. (For example, in some cultures a comma (,) separates groups of thousands.)

    A culture-specific decimal point symbol.
    So, focussing on the culture issue, converting 4.00 to 400 could be explained if your machine culture is set to use ',' as the decimal point symbol and '.' as the group-separator symbol.

  5. #5
    Join Date
    Aug 2007
    Posts
    2

    Re: Simple Casting problem

    zips is right.

    You can replace '.' with ',' in your code to see if this works for you:

    Code:
    string a = "4,00";
    double d = Convert.ToDouble(a);
    double d1 = d * 3;
    MessageBox.Show(d1.ToString());
    If your culture is set to use ',' as a decimal separator then you have to use ',' in all strings that have hard-coded, or you can always specify hard-coded doubles by using your current culture decimal separator like below.

    Of course, you also have to format the output string correctly using .ToString("N2") in order to see two decimals for the result.

    Code:
    System.Globalization.CultureInfo culture =
    System.Threading.Thread.CurrentThread.CurrentCulture;
    
    string dec_sep = culture.NumberFormat.NumberDecimalSeparator.ToString();
    
    string a = "4" + dec_sep + "00";
    double d = Convert.ToDouble(a);
    double d1 = d * 3;
    MessageBox.Show(d1.ToString("N2"));
    Last edited by cosmin.popescu; August 11th, 2007 at 02:36 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured