views:

53

answers:

5

I need to format a number like "1.2452" to show "452" So I don't want to see all numbers. The thing is, I am not able to control the display number directly, I can only format it by setting a format string of type according to the NumberFormatInfo Class. Is this possible, and if so how?

cheers

EDIT:

I found out I could subscribe to a "FormatEvent", which enabled me to make custom format as a string after the number formatting. Thanks everybody for the help and insight! :)

A: 

I'm pretty sure that's not possible because you can only filter out from least to most significant.

Jonas Elfström
A: 

I guess we would need to know exactly what algorithm you need to format the number. But you could convert it to a string and then just do a substring.

Remy
+1  A: 

You can create your own formatting specifiers by implementing IFormatter. But you cannot do so for the types that are already baked into .NET. Like System.Double or System.Decimal, or whatever the type of the variable whose value is 1.2452. The format specifiers that are baked in for these standard .NET types don't allow picking off the 3 last digits.

A custom culture with an altered NumberFormatInfo doesn't give you enough control over the formatting.

You'll need to get control over the actual value. Then it is simple.

Hans Passant
A: 

using .net

[math]::pi -- give 3.1415926538979

([math]::pi).tostring().split(".")[1] -- gives: 14159265358979

([math]::pi).tostring().split(".")[1].substring(0,4) -- Gives : 1415

Tom Ray
Using .NET? Are you sure about that?!
Josh Stodola
A: 

NumberFormatInfo does not provide enough flexibility to accomodate this (bizarre) format. I would suggest just having a ReadOnly property that formats your number accordingly...

public string NumberFormatted
{
    get
    {
        string num = YourNumber.ToString();
        int index = num.IndexOf(".");
        if(index > -1 && index < num.Length - 2)
            num = num.Substring(index + 2);
        return num;
     }
}
Josh Stodola