Avoid scientific notation when casting numbers to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis Myrén

    Avoid scientific notation when casting numbers to string

    Hi.

    Is there a way to make sure that float, double and decimal data types
    never will be presented in a scientific notation?

    I have tried to round(Math.Roun d) float's to 7 decimals, double's to 15
    and decimals to 28 decimals, but that does not help.
    And the System.Globaliz ation.NumberFor matInfo class does not seem to provide
    a such function.


    Thank you

    Regards, Dennis


  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: Avoid scientific notation when casting numbers to string

    Hi,

    You could just play with format specification strings like in this MSDN
    example from the
    "Standard Numeric Format Strings" topic:

    using System;
    using System.Threadin g;
    using System.Globaliz ation;

    class Class1
    {
    static void Main()
    {
    Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-us");
    double MyDouble = 123456789;

    Console.WriteLi ne("The examples in en-US culture.\n");
    Console.WriteLi ne(MyDouble.ToS tring("C"));
    Console.WriteLi ne(MyDouble.ToS tring("E"));
    Console.WriteLi ne(MyDouble.ToS tring("P"));
    Console.WriteLi ne(MyDouble.ToS tring("N"));
    Console.WriteLi ne(MyDouble.ToS tring("F"));

    Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
    Console.WriteLi ne("The examples in de-DE culture.\n");
    Console.WriteLi ne(MyDouble.ToS tring("C"));
    Console.WriteLi ne(MyDouble.ToS tring("E"));
    Console.WriteLi ne(MyDouble.ToS tring("P"));
    Console.WriteLi ne(MyDouble.ToS tring("N"));
    Console.WriteLi ne(MyDouble.ToS tring("F"));
    }
    }


    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "Dennis Myr?n" <dennis.myren@g reydigital.no-spam.no> wrote in message
    news:blh2ia$la0 $1@news.tdcnorg e.no...[color=blue]
    > Hi.
    >
    > Is there a way to make sure that float, double and decimal data types
    > never will be presented in a scientific notation?
    >
    > I have tried to round(Math.Roun d) float's to 7 decimals, double's to 15
    > and decimals to 28 decimals, but that does not help.
    > And the System.Globaliz ation.NumberFor matInfo class does not seem to[/color]
    provide[color=blue]
    > a such function.
    >
    >
    > Thank you
    >
    > Regards, Dennis
    >
    >[/color]

    Comment

    • Dennis Myrén

      #3
      Re: Avoid scientific notation when casting numbers to string

      Thank you for the reply.
      However, it is not that simple.
      Not any of those format strings does help in my case.


      "Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.c om> wrote
      in message news:O$XjMdPiDH A.428@TK2MSFTNG P12.phx.gbl...[color=blue]
      > Hi,
      >
      > You could just play with format specification strings like in this MSDN
      > example from the
      > "Standard Numeric Format Strings" topic:
      >
      > using System;
      > using System.Threadin g;
      > using System.Globaliz ation;
      >
      > class Class1
      > {
      > static void Main()
      > {
      > Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-us");
      > double MyDouble = 123456789;
      >
      > Console.WriteLi ne("The examples in en-US culture.\n");
      > Console.WriteLi ne(MyDouble.ToS tring("C"));
      > Console.WriteLi ne(MyDouble.ToS tring("E"));
      > Console.WriteLi ne(MyDouble.ToS tring("P"));
      > Console.WriteLi ne(MyDouble.ToS tring("N"));
      > Console.WriteLi ne(MyDouble.ToS tring("F"));
      >
      > Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
      > Console.WriteLi ne("The examples in de-DE culture.\n");
      > Console.WriteLi ne(MyDouble.ToS tring("C"));
      > Console.WriteLi ne(MyDouble.ToS tring("E"));
      > Console.WriteLi ne(MyDouble.ToS tring("P"));
      > Console.WriteLi ne(MyDouble.ToS tring("N"));
      > Console.WriteLi ne(MyDouble.ToS tring("F"));
      > }
      > }
      >
      >
      > --
      > Dmitriy Lapshin [C# / .NET MVP]
      > X-Unity Test Studio
      > http://x-unity.miik.com.ua/teststudio.aspx
      > Bring the power of unit testing to VS .NET IDE
      >
      > "Dennis Myr?n" <dennis.myren@g reydigital.no-spam.no> wrote in message
      > news:blh2ia$la0 $1@news.tdcnorg e.no...[color=green]
      > > Hi.
      > >
      > > Is there a way to make sure that float, double and decimal data types
      > > never will be presented in a scientific notation?
      > >
      > > I have tried to round(Math.Roun d) float's to 7 decimals, double's to 15
      > > and decimals to 28 decimals, but that does not help.
      > > And the System.Globaliz ation.NumberFor matInfo class does not seem to[/color]
      > provide[color=green]
      > > a such function.
      > >
      > >
      > > Thank you
      > >
      > > Regards, Dennis
      > >
      > >[/color]
      >[/color]


      Comment

      • Dmitriy Lapshin [C# / .NET MVP]

        #4
        Re: Avoid scientific notation when casting numbers to string

        Dennis,

        Maybe it's just I don't understand what you are trying to achieve? Could you
        please elaborate?

        --
        Dmitriy Lapshin [C# / .NET MVP]
        X-Unity Test Studio

        Bring the power of unit testing to VS .NET IDE

        "Dennis Myr?n" <dennis.myren@g reydigital.no-spam.no> wrote in message
        news:blj9k6$gnh $1@news.tdcnorg e.no...[color=blue]
        > Thank you for the reply.
        > However, it is not that simple.
        > Not any of those format strings does help in my case.
        >
        >
        > "Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.c om> wrote
        > in message news:O$XjMdPiDH A.428@TK2MSFTNG P12.phx.gbl...[color=green]
        > > Hi,
        > >
        > > You could just play with format specification strings like in this MSDN
        > > example from the
        > > "Standard Numeric Format Strings" topic:
        > >
        > > using System;
        > > using System.Threadin g;
        > > using System.Globaliz ation;
        > >
        > > class Class1
        > > {
        > > static void Main()
        > > {
        > > Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-us");
        > > double MyDouble = 123456789;
        > >
        > > Console.WriteLi ne("The examples in en-US culture.\n");
        > > Console.WriteLi ne(MyDouble.ToS tring("C"));
        > > Console.WriteLi ne(MyDouble.ToS tring("E"));
        > > Console.WriteLi ne(MyDouble.ToS tring("P"));
        > > Console.WriteLi ne(MyDouble.ToS tring("N"));
        > > Console.WriteLi ne(MyDouble.ToS tring("F"));
        > >
        > > Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
        > > Console.WriteLi ne("The examples in de-DE culture.\n");
        > > Console.WriteLi ne(MyDouble.ToS tring("C"));
        > > Console.WriteLi ne(MyDouble.ToS tring("E"));
        > > Console.WriteLi ne(MyDouble.ToS tring("P"));
        > > Console.WriteLi ne(MyDouble.ToS tring("N"));
        > > Console.WriteLi ne(MyDouble.ToS tring("F"));
        > > }
        > > }
        > >
        > >
        > > --
        > > Dmitriy Lapshin [C# / .NET MVP]
        > > X-Unity Test Studio
        > > http://x-unity.miik.com.ua/teststudio.aspx
        > > Bring the power of unit testing to VS .NET IDE
        > >
        > > "Dennis Myr?n" <dennis.myren@g reydigital.no-spam.no> wrote in message
        > > news:blh2ia$la0 $1@news.tdcnorg e.no...[color=darkred]
        > > > Hi.
        > > >
        > > > Is there a way to make sure that float, double and decimal data types
        > > > never will be presented in a scientific notation?
        > > >
        > > > I have tried to round(Math.Roun d) float's to 7 decimals, double's to[/color][/color][/color]
        15[color=blue][color=green][color=darkred]
        > > > and decimals to 28 decimals, but that does not help.
        > > > And the System.Globaliz ation.NumberFor matInfo class does not seem to[/color]
        > > provide[color=darkred]
        > > > a such function.
        > > >
        > > >
        > > > Thank you
        > > >
        > > > Regards, Dennis
        > > >
        > > >[/color]
        > >[/color]
        >
        >[/color]

        Comment

        • Jon Skeet

          #5
          Re: Avoid scientific notation when casting numbers to string

          Dennis Myrén <dennis.myren@g reydigital.no-spam.no> wrote:[color=blue]
          > Thank you for the reply.
          > However, it is not that simple.
          > Not any of those format strings does help in my case.[/color]

          The "F" specifier should be fine for you, but you'll need to specify
          how many decimal places you want.

          Note that .NET doesn't give you any easy way of formatting the *exact*
          value of a double (or float) as a string. If you want that, you can use
          the code I've got on
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Dennis Myrén

            #6
            Re: Avoid scientific notation when casting numbers to string


            Thank you all for your help.
            I just didnt realize i could specify the number of decimals as well, when
            using the "f"
            format string.

            Now it works.

            Regards, Dennis


            "Dennis Myrén" <dennis.myren@g reydigital.no-spam.no> wrote in message
            news:blj9k6$gnh $1@news.tdcnorg e.no...[color=blue]
            > Thank you for the reply.
            > However, it is not that simple.
            > Not any of those format strings does help in my case.
            >
            >
            > "Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.c om> wrote
            > in message news:O$XjMdPiDH A.428@TK2MSFTNG P12.phx.gbl...[color=green]
            > > Hi,
            > >
            > > You could just play with format specification strings like in this MSDN
            > > example from the
            > > "Standard Numeric Format Strings" topic:
            > >
            > > using System;
            > > using System.Threadin g;
            > > using System.Globaliz ation;
            > >
            > > class Class1
            > > {
            > > static void Main()
            > > {
            > > Thread.CurrentT hread.CurrentCu lture = new CultureInfo("en-us");
            > > double MyDouble = 123456789;
            > >
            > > Console.WriteLi ne("The examples in en-US culture.\n");
            > > Console.WriteLi ne(MyDouble.ToS tring("C"));
            > > Console.WriteLi ne(MyDouble.ToS tring("E"));
            > > Console.WriteLi ne(MyDouble.ToS tring("P"));
            > > Console.WriteLi ne(MyDouble.ToS tring("N"));
            > > Console.WriteLi ne(MyDouble.ToS tring("F"));
            > >
            > > Thread.CurrentT hread.CurrentCu lture = new CultureInfo("de-DE");
            > > Console.WriteLi ne("The examples in de-DE culture.\n");
            > > Console.WriteLi ne(MyDouble.ToS tring("C"));
            > > Console.WriteLi ne(MyDouble.ToS tring("E"));
            > > Console.WriteLi ne(MyDouble.ToS tring("P"));
            > > Console.WriteLi ne(MyDouble.ToS tring("N"));
            > > Console.WriteLi ne(MyDouble.ToS tring("F"));
            > > }
            > > }
            > >
            > >
            > > --
            > > Dmitriy Lapshin [C# / .NET MVP]
            > > X-Unity Test Studio
            > > http://x-unity.miik.com.ua/teststudio.aspx
            > > Bring the power of unit testing to VS .NET IDE
            > >
            > > "Dennis Myr?n" <dennis.myren@g reydigital.no-spam.no> wrote in message
            > > news:blh2ia$la0 $1@news.tdcnorg e.no...[color=darkred]
            > > > Hi.
            > > >
            > > > Is there a way to make sure that float, double and decimal data types
            > > > never will be presented in a scientific notation?
            > > >
            > > > I have tried to round(Math.Roun d) float's to 7 decimals, double's to[/color][/color][/color]
            15[color=blue][color=green][color=darkred]
            > > > and decimals to 28 decimals, but that does not help.
            > > > And the System.Globaliz ation.NumberFor matInfo class does not seem to[/color]
            > > provide[color=darkred]
            > > > a such function.
            > > >
            > > >
            > > > Thank you
            > > >
            > > > Regards, Dennis
            > > >
            > > >[/color]
            > >[/color]
            >
            >[/color]


            Comment

            Working...