English Number to Myanmar Number (Swift)

English နံပတ္ကေန ျမန္မာ နံပတ္ကို ျပန္ေျပာင္းဖုိ႕အတြက္ အတိုဆံုး ပံုစံ နဲ႕ ဘယ္လို ေရးလို႕ ရမလဲဆိုျပီး စမ္းၾကည့္ထားတာပါ။

Code

var eng = 890

var k = "\(eng)".map { UnicodeScalar(String($0))!.value + 4112 }.reduce("") { "\($0)\(String(Character(UnicodeScalar($1)!)))" }
print(k)

Swift မွာ အဆင္ေျပတာက reduce က ပထမဆံုး အခန္းကေန စတယ္။ reduce("") ျဖစ္သည့္အတြက္ previous value က empty string ပဲျဖစ္ေနေသးတယ္။

အကယ္၍ အႏႈတ္ ၊ဒႆမ ေတြ ပါလာရင္ေတာ့

var k = "\(eng)".map { (char) -> Int in
    let code = Int(UnicodeScalar(String(char))!.value)
    return (char == "." || char == "-") ? code : code + 4112
}.reduce("") { "\($0)\(String(Character(UnicodeScalar($1)!)))" }

ဆိုျပီး ေရးႏိုင္ပါတယ္။

Int , Double စတာေတြထဲမွာ extension အေနနဲ႕ သံုးမယ္ ဆုိရင္

import Foundation

protocol MyanNum {}

extension MyanNum {
    func toMyanmarNumber() -> String? {
        
        guard let this = self as? NSNumber else {
            return nil
        }
        let value =  "\(this)".map { (char) -> Int in
            let code = Int(UnicodeScalar(String(char))!.value)
            return (code >= 48 && code <= 57) ? code + 4112 : code
            }.reduce("") { "\($0)\(String(Character(UnicodeScalar($1)!)))" }
        return value
    }
}

extension Int : MyanNum {}
extension Double : MyanNum {}

print(76.toMyanmarNo())
print(22.987.toMyanmarNo())

အဲဒီအခါမွာ Int, Double ေတြကို တိုက္ရိုက္ ေခၚျပီးေတာ့ ျမန္မာ ဂဏန္း တန္ဖိုး ထုတ္ႏိုင္ပါတယ္။


 
3 Kudos
Don't
move!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.