2

I have the variable called values = "2". Now when the value length == 1 i need to add 0 at before the value.And if the value length ==2 no need to do any thing.How can i add 0 at before.

And when i try to convert int to string.And when i get the last 6 digit Like :

Number = "123456789"

Here when i try to get last 5 digit value.Then its printing like :

[ "7"  "8" "9" ")"  """  ]

How to extract numbers alone..Here my code :

let Acc = info.TotalNo
  let U = "\(Acc)"
let AccountNos = String(U.characters.suffix(5))
let list = AccountNos.characters.map { String($0) }

Then its printing like this :

[ "7"  "8" "9" " closeBracket"  " double colon "  ]

But i want :

[ "5"  "6" "7" "8"  "9"  ]

I know 2 question is not allowed..But tis two making me to break more code..Please give me some solution..

Thanks

doubtman
  • 61
  • 13
  • var newStr:String = str [which have value "0"] + [yourString] as you are able to get string count so try using that – iOS Geek Aug 01 '17 at 06:40
  • Are you sure to let Acc = info.TotalNo getting number is perfect? – Arjun Yadav Aug 01 '17 at 06:47
  • Your question is quite confusing. How is the value in `Number` related to the output with the closing parenthesis? – vadian Aug 01 '17 at 06:53
  • Possible duplicate of [Leading zeros for Int in Swift](https://stackoverflow.com/questions/25566581/leading-zeros-for-int-in-swift) – junaidsidhu Aug 01 '17 at 07:04

6 Answers6

3

you can format your number like this:

let format = "%02d"

let smallNumber = 2
let largerNumber = 13

let formattedSmallNumber = String(format: format, smallNumber)
// "02"
let formattedLargerNumber = String(format: format, largerNumber)
// "13"
André Slotta
  • 13,774
  • 2
  • 22
  • 34
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:27
1

Update: You are getting an optional so this code should run fine

if let Acc = info.TotalNo {
  let U = "\(Acc)"
  let AccountNos = String(U.characters.suffix(5))
  let list = AccountNos.characters.map { String($0) }
}

I made some changes in code

let number = 123456789

let Acc = "\(number)"  //let U = "\(Acc)"
let AccountNos = String(Acc.characters.suffix(5))
let list = AccountNos.characters.map { String($0) }

print(list)

I think problem with your solution is in this statement

let Acc = info.TotalNo

you are not getting the number

If you want last six digits

let number = "123456789"
let AccountNos = String(number.characters.suffix(6))
let list = AccountNos.characters.map { String($0) }

print(list)

Now to prefix 0 before number

let value = "2"
var newValue = ""

if value.characters.count == 1 {
    newValue = "0\(value)"
}

print(newValue)
3stud1ant3
  • 3,586
  • 2
  • 12
  • 15
  • My ` let U = "\(Acc)"` is printing `Optional("25578001")`.But my `let AccountNos = String(U.characters.suffix(5))` is printing `001")`..This is my isseu now – doubtman Aug 01 '17 at 07:10
  • try this: String(U!.characters.suffix(5)) – 3stud1ant3 Aug 01 '17 at 07:33
  • Hi any solution for here ...https://stackoverflow.com/questions/45605670/google-analytics-ecommerce-value-is-not-updating-in-google-analytics-console – doubtman Aug 10 '17 at 08:21
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:26
1

As for Your first question, there are two ways both works

Using NSMutableString:
var val: NSMutableString = "2"
if val.length == 1 {
  val.insert("0", at: 0)
}

Using String:
var val2 = "1"
if val2.characters.count == 1 {
   val2.insert("0", at: val2.startIndex)
}

For your second question

let number = "123456789"
let string = number.substring(from: number.index(number.startIndex, 
offsetBy: 4))
var list = Array(string.characters)
rajat chauhan
  • 178
  • 1
  • 8
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:27
0

Here you go for first part use

if values.characters.count < 2 {
    values = "0\(values)"
}

for second part you can use

var number = "123456789"
let Acc = "\(number)"
let AccountNos = String(Acc.characters.suffix(5))
let list = AccountNos.characters.map { String($0) }
Mohammad Sadiq
  • 5,070
  • 28
  • 29
0
  1. Answer var values = "2" let length = values.characters.count if length == 1 { values = "0" + values }

And if want you to convert the string to Int or double let doubleVal = Double(values)

anamika41
  • 161
  • 7
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:27
0

First Approach

for myInt in 1...100 {
    print(String(format: "%02d", myInt))
}

Second Approach using arguments

let string0 = String(format: "%02d", arguments: [0]) // returns "00"
let string1 = String(format: "%02d", arguments: [1]) // returns "01"
let string2 = String(format: "%02d", arguments: [10]) // returns "10"
let string3 = String(format: "%02d", arguments: [100]) // returns "100"

Third Approach using NumberFormatter

let formatter = NumberFormatter()
formatter.minimumIntegerDigits = 2

let optionalString0 = formatter.string(from: 0) // returns Optional("00")
let optionalString1 = formatter.string(from: 1) // returns Optional("01")
let optionalString2 = formatter.string(from: 10) // returns Optional("10")
let optionalString3 = formatter.string(from: 100) // returns Optional("100")

Reference link Leading zeros for Int in Swift

junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
  • any solution for here https://stackoverflow.com/questions/45667948/how-to-add-followed-text-to-uitextfield – doubtman Aug 14 '17 at 10:27