Home > Software engineering >  A floatting point value will have 2 digits after the decimal place and will occupy 8 places
A floatting point value will have 2 digits after the decimal place and will occupy 8 places

Time:11-22

Suppose, I have a float value [-12.3456]. I want to show it like [ -12.34].

I wrote the following:

    public override string ToString()
    {
        return $"{x:0.00, 8}{y:0.00, 8}{z:0.00, 8}";
    }

but, the output is not what I expect.

How can I do this?

CodePudding user response:

If I understood your question correctly, it should be

float f = (float)-12.3456;
string str = f.ToString("0.00").PadLeft(8);

CodePudding user response:

I found the answer:

using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine ("[{0, 10:0.00}]", -12.3456);
    }
}
  •  Tags:  
  • c#
  • Related