Giter Club home page Giter Club logo

arabic_persian-numbers-in-swift's Introduction

Arabic_Persian-Numbers-in-Swift

Swift 4.0 Xcode 10.0+ Platforms

Requirements

  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Swift 4 , Swift 3 , Swift 2.3

With this tutorial can convert Latin numbers to Arabic and Persian numbers in swift

    func englishToPersian() -> String{
        var sum = ""
        let letters = self.characters.map { String($0) }
        for letter in letters {
            if (Int(letter) != nil) {
                let persianNumber = ["۰","۱","۲","۳","۴","۵","۶","۷","۸","۹"]
                sum = sum+persianNumber[Int("\(letter)")!]
            } else {
                sum = sum+letter
            }
        }
        return sum
    }
    
    func persianToEnglish() -> String{
        var sum = ""
        let letters = self.characters.map { String($0) }
        let persianNumber = ["۰","۱","۲","۳","۴","۵","۶","۷","۸","۹"]
        for letter in letters {
            var isSuccess = false
            for (index, element) in persianNumber.enumerated() {
                if letter == element {
                    sum = sum + "\(index)"
                    isSuccess = true
                }
            }
            if isSuccess != true {
                sum = sum + letter
            }
        }
        return sum
    }
    
        let strEnglish1 = "1234567890"
        strEnglish1.englishToPersian() // ۱۲۳۴۵۶۷۸۹۰

        let strEnglish2 = "1200 toman"
        strEnglish2.englishToPersian() // ۱۲۰۰ toman

        let strPersian1 = "۱۲۳۴۵۶۷۸۹۰"
        strPersian1.persianToEnglish() // 1234567890

        let strPersian2 = "۱۲۰۰  تومان"
        strPersian1.persianToEnglish() // 1200  تومان

Contact

Follow and contact me on Twitter

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.