Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi i tryed sum codes for change number format but i didnt get 3 digits number

my deg "18"
i want "018"

What I have tried:

Dim deg = .Rows(0).Cells(0).Value

                MsgBox(deg.ToString("000"))
Posted
Updated 29-Jun-20 1:06am
v2
Comments
MadMyche 29-Jun-20 7:33am
   
Perhaps you should give better descriptions and definitions of your values and expectations.
Rate this:
Please Sign up or sign in to vote.

Solution 3

In addition to Garth's suggestion, don't just use Dim and hope the system will sort it out - it can't infer from the Value property what type deg should be. Specify exactly what type you are expecting, and it'll start to work.

Dim deg1 = gridSelectMusician.Rows(0).Cells(0).Value

Debug.WriteLine(deg1.ToString("000"))
Debug.WriteLine(deg1.ToString("0.00"))
Will give you this:
1
1
Because deg1 is an Object so it uses Object.ToString
Dim deg2 As Double = gridSelectMusician.Rows(0).Cells(0).Value

Debug.WriteLine(deg2.ToString("000"))
Debug.WriteLine(deg2.ToString("0.00"))
Will give you a better result:
001
1.23
Because it can use the Double.ToString override that accepts a format parameter.

It's generally a damn good idea to always specify your types - letting the system sort it out can cause all sorts of problems! (And is just one of the reasons VB is often seen as a childish language)
   
v2
Comments
Garth J Lancaster 29-Jun-20 8:39am
   
yes, I did wonder about that myself - my mind went back to the recent C# 'var' discussion !
Rate this:
Please Sign up or sign in to vote.

Solution 1

I would have thought

String.Format("{0:0.000}", deg)
   
v2
Comments
Member 14588284 29-Jun-20 6:58am
   
itried it like
MsgBox(String.Format("{000}", deg))
but its same value
deg=18
String.Format("{000}", deg) =18
String.Format("{0:0.000}", deg) = 18
Garth J Lancaster 29-Jun-20 7:09am
   
sorry - misread your question

what about
deg.ToString().PadLeft(3, "0"c)


- its the .PadLeft()... that's important here
Member 14588284 29-Jun-20 7:16am
   
thank you very much.
it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100