Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Changing the precision of a double

  • 14-07-2004 11:24am
    #1
    Registered Users, Registered Users 2 Posts: 2,758 ✭✭✭


    Folks,

    I'm hoping this is a simple or stupid question... I need to change the precision that a double shows on screen. I want to force it always to be 2 decimal places shown.

    I've checked msdn... no mention... googled... no joy. I remember being able to do it in C with floats.... how about it lads?

    ----

    I'm using C# with ... .Net 1.1 (i think)


Comments

  • Registered Users, Registered Users 2 Posts: 1,368 ✭✭✭king_of_inismac


    Ok, heres what I'd do.

    //variables
    char tempString[30];
    double temp = 50;

    //this converts the double to a string with 2 decimal places

    sprintf(&tempString,"%1.2f", temp);

    //display tempstring to the display//




    Hope this helps!,

    Martin


  • Registered Users, Registered Users 2 Posts: 629 ✭✭✭str8_away


    There is no stupid question as I always say.

    You could try "round" function.
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdecimalclassroundtopic.asp

    Hope this helps


  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    The sprintf solution will probably (I don't use C# so I'm not sure) truncate the double after the second decimal place so 2.099 should become 2.09.

    The round solution gives a correctly rounded number but the result will only have decimal places if they exist. 2.099 will round to 2.1 which won't print with two decimals without a sprintf.

    If sprintf truncates the double, then it's probably best to sprintf a round'ed double if you want the best of both worlds.


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Oof.

    Easy to see there's no C# programmer answered it yet ;

    Peace...have a look at the ToString overrides available on the double (and/or Double) types.

    You will see that as well as :

    myDouble.ToString()

    you can also have

    myDouble.ToString(formatString)

    There are also more complex options which allow you to do all sorts of funky items, but you shouldn't need much.

    What should do it for you is :

    myDouble.ToString("0.00");

    This will guarantee one place before and two places after the decimal place. Strangely, to the left of hte decimal place, 0's simply define the *minimum* number of places to display, where as to the right of it they define the *absolute* number of places to display.

    Thus, the following code :

    double d = 2000.123;
    Console.WriteLine(d.ToString("0.00"));

    Will produce as its output :

    2000.12

    Ta-dah!

    I'd strongly suggest you check up on the formatting options in C# because this is central to effective data-presentation.

    Cheers,

    jc


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Oh...the formatting does auto-rounding as well in the usual manner. .5 and up will get rounded up, otherwise round down.

    So in my example before, if I changed the test-value from 200.123 to 2000.125, the output would be 2000.13.

    jc


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,426 ✭✭✭ressem


    Nope, no sprintf in c#.

    For this, look at the double.ToString() method.
    Can take an IFormatProvider class as a parameter.
    You're looking to use NumberFormatInfo.

    Slightly modified version of the MSDN sam ple.

    As you can tell, lets you fob off some regionalization stuff to Microsoft.

    Edit, to compare to Bonkey's method
    This uses filler 0's if it needs to at the end.
    It automatically rounds in the standard way.

    Advantage in being able to change precision in one place for the entire class. Same for currency, thousands markers etc. Too easy to skimp on.

    /Edit


    using System;
    using System.Globalization;

    class NumberFormatInfoSample {

    public static void Main() {
    // Gets a NumberFormatInfo associated with the en-US culture.
    NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;

    // Displays a negative value with the default number of decimal digits 2.
    double myDouble = 3.14159265358979323846;
    Console.WriteLine( myDouble.ToString( "N", nfi ) );

    // Displays the same value with four decimal digits.
    nfi.NumberDecimalDigits = 4;
    Console.WriteLine( myDouble.ToString( "N", nfi ) ); }
    }


  • Registered Users, Registered Users 2 Posts: 2,758 ✭✭✭Peace


    Originally posted by bonkey
    Oof.

    Easy to see there's no C# programmer answered it yet ;

    Peace...have a look at the ToString overrides available on the double (and/or Double) types.

    You will see that as well as :

    myDouble.ToString()

    you can also have

    myDouble.ToString(formatString)

    There are also more complex options which allow you to do all sorts of funky items, but you shouldn't need much.

    What should do it for you is :

    myDouble.ToString("0.00");

    This will guarantee one place before and two places after the decimal place. Strangely, to the left of hte decimal place, 0's simply define the *minimum* number of places to display, where as to the right of it they define the *absolute* number of places to display.

    Thus, the following code :

    double d = 2000.123;
    Console.WriteLine(d.ToString("0.00"));

    Will produce as its output :

    2000.12

    Ta-dah!

    I'd strongly suggest you check up on the formatting options in C# because this is central to effective data-presentation.

    Cheers,

    jc

    Nailed it... nice one :)

    Cheers everybody....


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    God I love C#. Haven't seen CultureInfo and related classes being used before but I'll be using them from now on :)


Advertisement