Double ToString format string

E

Ed S

I'm trying to format a double with this behavior:

1) should only show 8 characters (character count before and after
decimal added together).
2) if greater than 7 characters to the left of the decimal, should
display in scientific notation.
3) if 7 characters or less to the left of the decimal, should round the
characters to the right of the decimal so only 8 characters are shown.

Anyone have any idea how to do this with the Double.ToString() format
string? Obviously using "e" as the format argument to ToString() will
always produce scientific notation, but that's not what I want in all
cases. Examples of what I'd like are:

orignal value desired value
------------- -------------
123.0 123.0
1234567.891 1234567.9
123456789.987 1.23e+08
123.456789123 123.45679

Thanks in advance for any help

--Ed
 
G

Guest

Ed
Use the string method indexof with the decimal as an argument. This will tell you where the decimal is and then you can decide what to do from there, if the indexof returns 9 or greater then you need to convert to sci not, but if index of is 8 or less then you need to do your rounding algo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top