VB.Net, formattting strings

I just had my question answer on how to format into currency in VB.Net as such

dim d as decimal
d = 254,31
format(d,"c")

Now I need to know more general ToString functions.

For example, suppose I have a list of values like 127.89, 127.9113, and 129.332, .3347

I want those to appear as
127.8900
127.9113
129.3320
    0.3347

In C# I can use d.tostring("##0.0000") to get those results.  How do I do that in VB.Net?
LVL 4
g_johnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nepaluzCommented:
Try

Formatnumber("127.89",4)
0
nepaluzCommented:
Even

Formatnumber(d, 4)
0
Anil GolamariCommented:
if you want to format it in .tostring

then use this code    theDecNumber.ToString("N4")

for ref: http://www.programmers-corner.com/sourcecode/125
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

ladarlingCommented:
This is how I would do it:

For each s as string in MyStringArray
Dim d=cdec(s)
s = d.tostring("0.0000" )
Next
0
HainKurtSr. System AnalystCommented:
check here

String format
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

custom number formats
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

and use like:

Dim formatString As String = String.Format("{0,10:G}: {0,10:X}", value)
0
g_johnsonAuthor Commented:
Here is exactly what I am trying to do.  Combined with a Courier New Font, this will give me a visibly pleasant string to put in a list.  :)

Dim sRecValue As String = RecValue.ToString("##0.0000")

However, this won't compile:  Overload resolution failed because no accessible 'ToString' accepts this number of arguments.

I am on the 3.5 Framework.

I read the stuff HainKurt references, but am not "getting it"  For example, I cannot read this line of code:
Dim formatString As String = String.Format("{0,10:G}: {0,10:X}", value).  I don't know how to interpret that.


0
Anil GolamariCommented:
http://www.programmers-corner.com/sourcecode/125

take a look at this site and you have all kinds of format methods you want regarding the decimal places.
0
nepaluzCommented:
You are a stubborn man......

Dim sRecValue As String = FormatNumber("127.89", 4).ToString

even

Dim sRecValue As String = FormatNumber("127.89", 4).ToString
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
I think what you are using in C# it works also in vb.net

Dim DecStr As Decimal = 127.89
Me.Label1.Text = DecStr.ToString("##0.0000")
0
g_johnsonAuthor Commented:
nepauluz -- thanks for the nice comment, but your solution does not work as it does not account for the leading blanks.  It's fine for everything to the right of the decimal.

jtouttou -- whenever I put anything into .ToString as we do in C#, I get the complie error: Overload resolution failed because no accessible 'ToString' accepts this number of arguments.

lucky85:  thank you also, but same problems as listed above.

When I am done formatting I need it to be exactly X digits long, including leading spaces.  I'm not finding a way to do that.
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Dim DecStr As Decimal = 127.89
Me.Label1.Text = DecStr.ToString("##0.0000") works to me
0
g_johnsonAuthor Commented:
jtoutou -- Am I maybe missing a reference in my class?  Do you know what referenceI should be importing?  I keep getting the error.
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
nothing i just declare the DecStr as decimal
0
g_johnsonAuthor Commented:
Okay, thanks -- I expected it to work, also, but I get this all the time:

Overload resolution failed because no accessible 'ToString' accepts this number of arguments.
when I do d.ToString("##,##0.00").

I don't get it!  :-)
0
nepaluzCommented:
What leading zeroes are you refering to?
Anyway, i give up.
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Dim DecStr As Decimal = 127.89
        Me.Label1.Text = DecStr.ToString("##0.0000") not ("##,##0.00")
0
g_johnsonAuthor Commented:
jtoutou:

Good catch, but same problem.  It won't compile.
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim DecStr As Decimal = 127.89
        Me.Label1.Text = DecStr.ToString("##0.0000")
    End Sub.
Screen01.JPG
0
ladarlingCommented:
Dude... its not that hard, my example implied that you simply pad the number with zeros when calling any floating point types .ToString() method:
 
If you need it padded to 8 places, for example:

Dim s As String = "12.33455"
Dim d As Double = CDbl(s)
s = d.ToString("0000.0000")
MsgBox(s)
If you dont do the numeric conversion you are going to have ot pad the string manually using the format() function.
0
g_johnsonAuthor Commented:
jtoutyou -- Thanks for your patience and persistence.

I have found the issue. This works:


            Dim d As Decimal
            d = Convert.ToDecimal(123.89)
            MessageBox.Show(d.ToString("##,##0.00")

This does not:

            Dim d? As Decimal
            d = Convert.ToDecimal(123.89)
            MessageBox.Show(d.ToString("##,##0.00")

So, the fact that d is nullable is causing my problem.  Any idea how to overcome this?  (Sorry, I missed that fact earlier)
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
wherever youhve nulls set your desured format with zeros
0
ZhaolaiCommented:
Try this:

            Dim d? As Decimal
            d = Convert.ToDecimal(123.89)
            MessageBox.Show(d.Value.ToString("##,##0.00")

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
g_johnsonAuthor Commented:
Zhaolai:  Thanks --
It wouldn't respect the leading spaces, so this is what ultimately works:
RecValue.Value.ToString(".00").PadLeft(9)

using the .Value was what I needed to make it compile

0
g_johnsonAuthor Commented:
Most of the others were missing the need for the leading spaces.  Thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.