Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

مخرجات منسقة

جزء من قسم الأساسيات في رحلة Swift على Coddy — الدرس 41 من 86.

challenge icon

التحدي

سهل

في الدرس السابق، قمت بحساب المبلغ الذي يدفعه كل شخص. الآن، قم بتنسيق القيم النقدية لعرض منزلتين عشريتين بالضبط للحصول على مخرجات احترافية ومصقولة.

استخدم String(format:) في لغة Swift لتنسيق قيم tipAmount و totalWithTip و amountPerPerson لإظهار منزلتين عشريتين بالضبط.

محدد التنسيق "%.2f" يقوم بتنسيق Double إلى منزلتين عشريتين:

let formatted = String(format: "%.2f", someDouble)

قم بتحديث مخرجاتك لتتم طباعتها بهذا التنسيق الدقيق:

Welcome to the Bill Split Calculator!
Bill: [billTotal]
Tip: [tipPercentage]%
People: [numberOfPeople]
Tip amount: $[tipAmount formatted]
Total with tip: $[totalWithTip formatted]
Each person pays: $[amountPerPerson formatted]

لاحظ أنه تمت إضافة رمز $ الآن قبل كل قيمة نقدية، وجميع المبالغ تظهر منزلتين عشريتين بالضبط.

على سبيل المثال، إذا كانت المدخلات هي 85.5 و 15 و 3، يجب أن تكون المخرجات:

Welcome to the Bill Split Calculator!
Bill: 85.5
Tip: 15%
People: 3
Tip amount: $12.83
Total with tip: $98.33
Each person pays: $32.78

جرّب بنفسك

print("Welcome to the Bill Split Calculator!")

if let billInput = readLine(), let billTotal = Double(billInput) {
    if let tipInput = readLine(), let tipPercentage = Int(tipInput) {
        if let peopleInput = readLine(), let numberOfPeople = Int(peopleInput) {
            let tipAmount = billTotal * Double(tipPercentage) / 100
            let totalWithTip = billTotal + tipAmount
            let amountPerPerson = totalWithTip / Double(numberOfPeople)
            
            print("Bill: \(billTotal)")
            print("Tip: \(tipPercentage)%")
            print("People: \(numberOfPeople)")
            print("Tip amount: \(tipAmount)")
            print("Total with tip: \(totalWithTip)")
            print("Each person pays: \(amountPerPerson)")
        }
    }
}

جميع دروس الأساسيات