Solved

VB.Net, formattting strings

Posted on 2010-08-16
24
466 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 51

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 100 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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 31
FInd Image Control Gridview 3 21
Open a link in vb.net 2 16
VB.net Adding a row to a DataGridView that has a checkbox column 5 22
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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