Solved

VB.Net, formattting strings

Posted on 2010-08-16
24
461 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now