tags:

views:

110

answers:

6

Hello,

In C#, is it possible to perform ToString on a float and get the value without using exponentials?

For example, consider the following:

float dummy;

dummy = 0.000006F;

Console.WriteLine(dummy.ToString());

This gives the output

6E-06

However, what I was is

0.000006

The closest I could find was using the "F" qualifier, however I then need to specify the number of decimal places otherwise the value get rounded.

Is there actually a way of doing this automatically or do I need to do a load of funky logic to either trim zeroes or figure out the number of required decimals.

Thanks;
Richard Moss

A: 
Console.WriteLine(dummy.ToString("N5"));

where 5 its number of decimal places

Sebastian Brózda
Thanks for the response, this is also something I've already tried and suffers the same issue as the F qualifier in that I need to explicitly specify a precision which I won't know.
Richard Moss
A: 

Try this

Console.WriteLine(dummy.ToString("F"));

You can also specify number of decimal places. For example F5, F3, etc.

Also, you can check custom format specifier

Console.WriteLine(dummy.ToString("0.#########"));
MAKKAM
Yes, as mentioned I already tried this. However, I need to specify the number of decimal places which I won't necessarily know (unless there's something else I'm missing ;)) - I'm somewhat surprised that I can't seem to do this except with the E notation which my users don't want.
Richard Moss
Oh, sorry, I had to read more carefully. So, if you want to display the float number you should declare maximal decimal precision to show. By the way, float type doesn't provide high level of precision (~10^(-39)) and you can just choose the big enough amount of decimal places
MAKKAM
I had also tried this, ie F10, but the "side effect" was that the zeroes continued after the 6. I had hoped to avoid this, but if it is going to be the only way I can strip of the extra zero's I suppose. It just seems so ... clunky!
Richard Moss
Maybe the custom format will help you. Try this: Console.WriteLine(dummy.ToString("0.#########")); it will print maximum 9 decimal places and cut trailing zeros
MAKKAM
BTW, .Net decimal type is preferable for decimal calculations
MAKKAM
@Richard have you considered using the decimal type instead? It sounds like this may be a better fit for what you want.
Winston Smith
While as you say it might make a lot of sense to use the decimal type unfortunately that would be a major change and not something I can do in the short term.
Richard Moss
A: 

You can use the following implementation I did?

public string GetFormattedValue(float fltParam, int numberOfDecimalPlaces)
{
    string format = "#.";
    for(int formatIndex = 0;formatIndex < numberOfDecimalPlaces;formatIndex++)
    {
        format += "#";
    }

    string strFlt = fltParam.ToString(format);

    if (strFlt.StartsWith("."))
        strFlt = "0" + newValue;
    if (strFlt.Lenght == 0)
        strFlt = "0";
    return newValue;
}

This is not the most effective code. But it will do the trick. :-).

Please also note the depending on your regional settings it might be a ',' instead of a '.'

Koekiebox
A regex will be better. But as you can see I am too lazy to do the regex, which leads to a whole other conversation.
Koekiebox
GetFormattedValue(0) returns empty string. I think the problem is a format specifier in which all the symbols are optional.
MAKKAM
Debugging by a special cases is not the best approach=) At least, this function doesn't support negative numbers. And, could you, please, tell me what characters do you want to filter by this for loop?
MAKKAM
I see what what you mean. The character filter was for a different ToString() (The default). LOL. This function is starting to look poor, very poor.
Koekiebox
A: 
       string dum = string.Format("{0:f99}",dummy).TrimEnd('0');
       if (dum.EndsWith(",")) dum = dum.Remove(dum.Length - 1);
Andrzej Nosal
A: 

Without some further background info, it's hard to tell - but it sounds like you want decimal semantics. So why not use the decimal type instead?

decimal dummy;
dummy = 0.000006M;

The decimal type is more accurate at representing decimal numbers than float or double, but it is not as performant. See here for more info.

Winston Smith
A: 
float dummy = 0.000006F;
Console.WriteLine(dummy.ToString("0." + new string('#', 60)));

If you'll be doing this a lot then it makes sense to store the format string in a static field/property somewhere and re-use it, rather than constructing a new string every time:

private static readonly string _allFloatDigits = "0." + new string('#', 60);

// ...

float dummy = 0.000006F;
Console.WriteLine(dummy.ToString(_allFloatDigits));
LukeH