Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
1.00/5 (1 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.

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.

VB
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
VB
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)
 
Share this answer
 
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 !
I would have thought

String.Format("{0:0.000}", deg)
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900