2

I need to check if the int is only one digit and if it is, I want to add a zero in front of it. I have this code but it doesnt work.

var minutes2 = Int(minutes) var minutessize: Int = sizeofValue(minutes2) if minutessize < 2 { var needStringHere = "0\(minutes2)" let a: Int? = needStringHere.toInt() minutes2 = a! } 
0

1 Answer 1

6

You can just check if the minutes count is less than 10:

var str = "\(minutes2)" if (minutes2 < 10) { str = "0\(minutes2)" } 
Sign up to request clarification or add additional context in comments.

2 Comments

yea but how do i convert the string to an int. The code doesnt work there
@DenizYazar var integer = Int(str)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.