C# - String formatting: double fixed width

拈花ヽ惹草 提交于 2019-12-10 23:43:11

问题


How can I use String.Format in C# so doubles are displayed like this:

Values:

-1.0
1.011
100.155
1000.25
11000.52221

displayed string:

-1.00
1.011
100.2
 1000
11001

The main point is my width is fixed to 5 characters no matter what. I don't really care how many decimal places are shown to the right. If there are 4 or more numbers to the left of decimal I want everything right of the decimal to be dropped (including the decimal itself).

It seems like something that should be a pretty standard practice. But I'm not having much luck finding an answer that works.

A couple of corrections were made to the display string above, I do want rounding.

Thanks!


回答1:


public string FormatNumber(double number)
{
    string stringRepresentation = number.ToString();

    if (stringRepresentation.Length > 5)
        stringRepresentation = stringRepresentation.Substring(0, 5);

    if (stringRepresentation.Length == 5 && stringRepresentation.EndsWith("."))
        stringRepresentation = stringRepresentation.Substring(0, 4);

    return stringRepresentation.PadLeft(5);
}

EDIT: Just realized that this doesn't pad zeros at the end of the decimal if necessary (as in your first example) but should give you the tools to finish it off as you need to.

EDITx2: Given your most recent addition that you intend to have rounding, it gets more complicated. First you have to do a check to see if you will have any decimal places and at what position the decimal is. Then you have to round it to that decimal place, then probably run through the output. Note that depending on your algorithm, you could get some incorrect results where rounding rolls over numbers (for example, -10.9999 could become -11.00 or -11 depending on your implementation)




回答2:


Usually this rule is applied on exchange market and I develop it as below:

if (number < 1)
   cell.Value = number.ToString("0.00000");
else if (number < 10)
   cell.Value = number.ToString("0.0000");
else if (number < 100)
   cell.Value = number.ToString("00.000");
else if (number < 1000)
   cell.Value = number.ToString("000.00");
else if (number < 10000)
   cell.Value = number.ToString("0000.0");
else if (number < 100000)
   cell.Value = number.ToString("00000");



回答3:


Create an extension method on Double if it's going to be used often and in many places.

using System;

public static class DoubleExtensionMethods
{
    public static string FormattedTo5(this double number)
    {
        string numberAsText = number.ToString();

        if (numberAsText.Length > 5)
        {
            numberAsText = numberAsText.Substring(0, 5);
        }

        return numberAsText.TrimEnd('.').PadLeft(5);
    }
}

Useage would then be:

double myDouble = 12345.6789D;

string formattedValue = myDouble.FormattedTo5();


来源:https://stackoverflow.com/questions/12642642/c-sharp-string-formatting-double-fixed-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!