?
Solved

VB.Net, formattting strings

Posted on 2010-08-16
24
Medium Priority
?
487 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:g_johnson
  • 8
  • 6
  • 4
  • +4
24 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 33447559
Try

Formatnumber("127.89",4)
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33447597
Even

Formatnumber(d, 4)
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33447598
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:ladarling
ID: 33447599
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 33447609
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
 
LVL 4

Author Comment

by:g_johnson
ID: 33447794
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
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33447853
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 33447861
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33447868
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
 
LVL 4

Author Comment

by:g_johnson
ID: 33449390
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449417
Dim DecStr As Decimal = 127.89
Me.Label1.Text = DecStr.ToString("##0.0000") works to me
0
 
LVL 4

Author Comment

by:g_johnson
ID: 33449493
jtoutou -- Am I maybe missing a reference in my class?  Do you know what referenceI should be importing?  I keep getting the error.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449535
nothing i just declare the DecStr as decimal
0
 
LVL 4

Author Comment

by:g_johnson
ID: 33449550
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 33449562
What leading zeroes are you refering to?
Anyway, i give up.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449609
Dim DecStr As Decimal = 127.89
        Me.Label1.Text = DecStr.ToString("##0.0000") not ("##,##0.00")
0
 
LVL 4

Author Comment

by:g_johnson
ID: 33449662
jtoutou:

Good catch, but same problem.  It won't compile.
0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 400 total points
ID: 33449743
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
 
LVL 11

Expert Comment

by:ladarling
ID: 33449762
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
 
LVL 4

Author Comment

by:g_johnson
ID: 33449818
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449914
wherever youhve nulls set your desured format with zeros
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 1600 total points
ID: 33449948
Try this:

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

0
 
LVL 4

Author Comment

by:g_johnson
ID: 33450105
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
 
LVL 4

Author Closing Comment

by:g_johnson
ID: 33450117
Most of the others were missing the need for the leading spaces.  Thanks for your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question