locked
How to round-off values RRS feed

  • Question

  • User-1909056490 posted

    Hi,

    How to round-off values 339.79499999999996 To 339.7950 

    I want output like 339.7950

    thanks in advance

    Sunday, August 24, 2014 4:26 AM

Answers

  • User281315223 posted

    You can use the Math.Round() method to indicate the number of decimal places that you want to round to if you need to keep the value as an decimal :

    // Your original string
    decimal d = 339.79499999999996m;
    
    // Round your value
    decimal dr = Math.Round(d, 4); // yields 339.795
    

    This should be equivalent to the value that you are looking for. If you need to explicitly output the additional zero, you'll need to do so as a string (since the 0 has no real value and will not be displayed by a numeric type).

    You can use the "N4" formatting string to explicitly output 4 decimal values (trailing zeros will be included) :

    // Your original string
    decimal d = 339.79499999999996m;
    
    // Include the trailing zero as an output string
    string roundedString = d.ToString("N4"); // yields "339.7950"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 24, 2014 9:23 AM
  • User-986818884 posted

    You can use Math.Round or ToString format or use String.Format

    Example Math.Round(333.333333,2) answer will be 333.33 (learn more from MSDN)

    Example ToString("0.00") will gives you same answer 333.33.. You can change no of Zero as per your requirement.

    String.Format("{0:0.00}", 123.4567);      // "123.46"
    String.Format("{0:0.00}", 123.4);         // "123.40"
    String.Format("{0:0.00}", 123.0)

    http://www.csharp-examples.net/string-format-double/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 25, 2014 6:54 AM

All replies

  • User281315223 posted

    You can use the Math.Round() method to indicate the number of decimal places that you want to round to if you need to keep the value as an decimal :

    // Your original string
    decimal d = 339.79499999999996m;
    
    // Round your value
    decimal dr = Math.Round(d, 4); // yields 339.795
    

    This should be equivalent to the value that you are looking for. If you need to explicitly output the additional zero, you'll need to do so as a string (since the 0 has no real value and will not be displayed by a numeric type).

    You can use the "N4" formatting string to explicitly output 4 decimal values (trailing zeros will be included) :

    // Your original string
    decimal d = 339.79499999999996m;
    
    // Include the trailing zero as an output string
    string roundedString = d.ToString("N4"); // yields "339.7950"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 24, 2014 9:23 AM
  • User-986818884 posted

    You can use Math.Round or ToString format or use String.Format

    Example Math.Round(333.333333,2) answer will be 333.33 (learn more from MSDN)

    Example ToString("0.00") will gives you same answer 333.33.. You can change no of Zero as per your requirement.

    String.Format("{0:0.00}", 123.4567);      // "123.46"
    String.Format("{0:0.00}", 123.4);         // "123.40"
    String.Format("{0:0.00}", 123.0)

    http://www.csharp-examples.net/string-format-double/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 25, 2014 6:54 AM