C# How to format a double to one decimal place without rounding

后端 未结 6 1367

I need to format a double value to one decimal place without it rounding.

double value = 3.984568438706
string result = \"\";

What I have tried

相关标签:
6条回答
  • 2021-02-18 15:50

    Just use modulo operator + built in ToString:

    result = (value - (value % 0.1)).ToString("N1") + "%";
    
    0 讨论(0)
  • 2021-02-18 15:56

    ToString() doesn't do it. You have to add extra code. The other answers show math approaches, my approach below is kind of outside-the-box.

    string result = value.ToString();
    Console.WriteLine("{0}", result.Substring(0, result.LastIndexOf('.') + 2));
    

    This is a fairly simple brute force approach, but it does the trick when the decimal is a '.'. Here's an extension method to ease the pain (and deals with the decimal point).

    public static class Extensions
    {
        public static string ToStringNoTruncate(this double me, int decimalplaces = 1)
        {
            string result = me.ToString();
            char dec = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
            return result.Substring(0, result.LastIndexOf(dec) + decimalplaces + 1);
        }
    }
    
    0 讨论(0)
  • 2021-02-18 15:58

    I know this is a old thread but I've just had to do this. While the approaches here work I want a easy way to be able to affect a lot of calls so using the Math.Truncate on all the calls to string.format wasn't really a good option.

    Thus, I made a custom format provider which would allow me to add truncation to the formatting string, eg

    string.format(new FormatProvider(), "{0:T}", 1.1299); // 1.12
    string.format(new FormatProvider(), "{0:T(3)", 1.12399); // 1.123
    string.format(new FormatProvider(), "{0:T(1)0,000.0", 1000.9999); // 1,000.9
    

    The implementation is pretty simple and is easily extendible to other requirements.

    public class FormatProvider : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type formatType)
        {
            if (formatType == typeof (ICustomFormatter))
            {
                return this;
            }
            return null;
        }
    
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (arg.GetType() != typeof (double))
            {
                try
                {
                    return HandleOtherFormats(format, arg);
                }
                catch (FormatException e)
                {
                    throw new FormatException(string.Format("The format of '{0}' is invalid.", format));
                }
            }
    
            if (format.StartsWith("T"))
            {
                int dp = 2;
                int idx = 1;
                if (format.Length > 1)
                {
                    if (format[1] == '(')
                    {
                        int closeIdx = format.IndexOf(')');
                        if (closeIdx > 0)
                        {
                            if (int.TryParse(format.Substring(2, closeIdx - 2), out dp))
                            {
                                idx = closeIdx + 1;
                            }
                        }
                        else
                        {
                            throw new FormatException(string.Format("The format of '{0}' is invalid.", format));
                        }
                    }
                }
                double mult = Math.Pow(10, dp);
                arg = Math.Truncate((double)arg * mult) / mult;
                format = format.Substring(idx);
            }
    
            try
            {
                return HandleOtherFormats(format, arg);
            }
            catch (FormatException e)
            {
                throw new FormatException(string.Format("The format of '{0}' is invalid.", format));
            }
        }
    
        private string HandleOtherFormats(string format, object arg)
        {
            if (arg is IFormattable)
            {
                return ((IFormattable) arg).ToString(format, CultureInfo.CurrentCulture);
            }
            return arg != null ? arg.ToString() : String.Empty;
        }
    }
    
    0 讨论(0)
  • 2021-02-18 16:08
    result=string.Format("{0:0.0}",Math.Truncate(value*10)/10);
    
    0 讨论(0)
  • 2021-02-18 16:08

    I would make a utility method to handle this:

    static double Truncate(double value, int digits)
    {
        double mult = System.Math.Pow(10.0, digits);
        return System.Math.Truncate(value * mult) / mult;
    }
    

    You could then do:

    result = Truncate(value, 1).ToString("##.#", System.Globalization.CultureInfo.InvariantCulture) + "%"; 
    

    Note that you may also want Math.Floor instead of truncate - but it depends on how you want negative values handled.

    0 讨论(0)
  • 2021-02-18 16:09
    ( Math.Truncate( ( value * 10 ) ) / 1000 ).ToString( "#.#%" )
    
    0 讨论(0)
提交回复
热议问题