Format method

M

mp

Trying to use the format method in vb.net
to oconvert a double to string of 4 places

I tried:
Format(CStr(dHeightOfStone), "x.xxxx")

and:

Format(CStr(dHeightOfStone), "#.####")

It returns "x.xxxx" or "#.####" instead of "36.1875" (or whatever number was
input)

any tips?

thanks mark
 
A

Armin Zingler

Am 17.03.2010 23:56, schrieb mp:
Trying to use the format method in vb.net
to oconvert a double to string of 4 places

I tried:
Format(CStr(dHeightOfStone), "x.xxxx")

and:

Format(CStr(dHeightOfStone), "#.####")

It returns "x.xxxx" or "#.####" instead of "36.1875" (or whatever number was
input)

any tips?

These format strings are for formatting numbers. You are trying
to format strings because, before, you've converted the numbers
to strings using the CStr function. Try this instead:

dHeightOfStone.ToString("#.####")
 
F

Family Tree Mike

Trying to use the format method in vb.net
to oconvert a double to string of 4 places

I tried:
Format(CStr(dHeightOfStone), "x.xxxx")

and:

Format(CStr(dHeightOfStone), "#.####")

It returns "x.xxxx" or "#.####" instead of "36.1875" (or whatever number was
input)

any tips?

thanks mark

I believe you want to use Format(dHeightOfStone, "#.####").

Frankly, I prefer dHeightOfStone.ToString("f4"), but that's just my style...
 
M

mp

ok, even though the help files show the order i first tried,
Format(CStr(dHeightOfStone), "#.####") i tried the opposite order
Format( "#.####", CStr(dHeightOfStone))
now instead of getting "#.####" returned, i get
4.62500000000364

so it's still not formatting
what am i missing?

as an alternative i tried using round
Dim sWidthLiner As String =
CStr(Decimal.Round(Cdec(dLengthOfMoldSidesLiner), 4))

but it seems a long way around to just get a double rounded to x places and
converted to string

thanks
mark
 
M

mp

Thanks Mike and Armin,

Family Tree Mike said:
I believe you want to use Format(dHeightOfStone, "#.####").

Frankly, I prefer dHeightOfStone.ToString("f4"), but that's just my
style...
 
F

Family Tree Mike

ok, even though the help files show the order i first tried,
now instead of getting "#.####" returned, i get
4.62500000000364

so it's still not formatting
what am i missing?

as an alternative i tried using round
Dim sWidthLiner As String =
CStr(Decimal.Round(Cdec(dLengthOfMoldSidesLiner), 4))

but it seems a long way around to just get a double rounded to x places and
converted to string

thanks
mark

Mark,

Did you not see Armin's and my responses?

You can use:

1. dHeightOfStone.ToString("#.####")
2. Format(dHeightOfStone, "#.####")
3. dHeightOfStone.ToString("f4")

but none of what you have posted will work. You need a number as the
first argument and a format string as the second. Turn "Option Strict
On" at the top of your code. You should get an error on some of the
things you are trying.
 
M

mp

Thanks Mike,
They came in after i posted my second stupid effort
:)
dumb newbie
:)
Thanks for the info
I've gone with your favorite dHeightOfStone.ToString("f4")
nice and concise

mark
 

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