How To Display Fractions and Special Characters in Swift and SwiftUI

How To Display Fractions and Special Characters in Swift and SwiftUI

NSLocalizedString takes a key and a comment and returns a localized value of a string. You can pass Unicode characters to NSLocalizedString and have your special character displayed inside your app.

NSLocalizedString

NSLocalizedString takes a key and a comment and returns a localized value of a string. You can pass Unicode characters to NSLocalizedString and have your special character displayed inside your app.

Example

To display a fraction, you can use a numerator Unicode, fraction slash Unicode, and a denominator Unicode.

The Unicode for numerators is \u{207x}. The x is a number in this set {0,4,5,6,7,8,9}. A fraction slash is \u{2044}. Lastly, the Unicode for denominators is \u{208x}. Unlike the numerators, the x is a number between 1 and 9. You can create a string variable that holds the fraction 4/5 in SwiftUI with the code below:

@State var num = "
    \(NSLocalizedString("\u{2074}", comment: "Numerators - 4"))
    \(NSLocalizedString("\u{2044}", comment: "fraction slash"))
    \(NSLocalizedString("\u{2085}", comment: "Denominator - 5"))
    "

There are existing Unicode characters for common fractions such as \u{00BD} for 1/2. You can use the function below to return the common fractions. Pass a double value to the function and it will return the Unicode character equivalent.

func fractionToString (fraction: Double) -> String {
             switch fraction {
             case 0.125..<0.126:
                 return NSLocalizedString("\u{215B}", comment: "1/8")
             case 0.25..<0.26:
                 return NSLocalizedString("\u{00BC}", comment: "1/4")
             case 0.33..<0.34:
                 return NSLocalizedString("\u{2153}", comment: "1/3")
             case 0.5..<0.6:
                 return NSLocalizedString("\u{00BD}", comment: "1/2")
             case 0.66..<0.67:
                 return NSLocalizedString("\u{2154}", comment: "2/3")
             case 0.75..<0.76:
                 return NSLocalizedString("\u{00BE}", comment: "3/4")
             default:
                return "\(fraction)"
            }
         }

swiftui swift ios-app-developers programming ios-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Top Swift iOS App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Best Swift App Development Company

Apple Swift App Development India: Mobile App Development India is an expert iOS Swift App Development company in India. We develop secure and robust mobile app solutions based on new Apple Swift language.

Top-Notch Swift iOS App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Hire iOS App Developer

Are you looking to transform your idea into an iPhone application? Hire iPhone programmer team from **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** to ensure the best results, utilizing all the latest trends in iOS app...