Swift Program to Round a Number to n Decimal Places


This tutorial will discuss how to write a Swift program to round a number to n decimal places.

Rounding a number means round a decimal number to a certain number of decimal places or we can say round to the nearest wholes, tenths, hundredths, or thousandth. It save time and helps to express long term into short term. Rounding is only applicable on Float and Double data types. When you round a number always remember the following points −

If the last digit is less than 5(that is 0, 1, 2, 3, 4) then round the previous digit down. For example, 52.12 rounded to the nearest tenth place that is 52.1 because 1 is followed by 2 and 2 is less than 5.

If the last digit is greater than 5(that is 5, 6, 7, 8, 8) then round the previous digit up. For example, 52.18 rounded to the nearest tenth place that is 52.2 because 1 is followed by 9 and 9 is greater than 5.

Below is a demonstration of the same −

Input

Suppose our given input is −

Entered number - 456.3478

Output

The desired output would be −

Rounded number(hundredth place)- 456.35

In Swift we can round a number using any of the following methods −

Method 1 - Using Round Function

Swift provide a built-in function named as round() function. This function is used to round the given number to the nearest whole number. It accept both Float and Double.

Syntax

Following is the syntax −

func round(number)

Example 1

The following program shows how to round a number to n decimal places using round() function.

import Swift import Foundation var n1 : Float = 6.98 var n2 : Double = 876.876 var n3 : Double = -0.987 // Floating point number print("Round number \(n1):", round(n1)) // Positive double number print("Round number \(n2):", round(n2)) // Negative double number print("Round number \(n3):", round(n3))

Output

Round number 6.98: 7.0
Round number 876.876: 877.0
Round number -0.987: -1.0

Example 2

The following program shows how to round a number to n decimal places using round() function.

import Swift import Foundation var n1 : Float = 34.459 var n2 : Double = 7.89098 var n3 : Double = -3.96396238638 // Round Floating point number to one(tenths) decimal place print("Round number \(n1):", round(n1 * 10)/10.0) // Round positive double number to two(hundredths) decimal place print("Round number \(n2):", round(n2 * 100)/100.0) // Round negative double number to three(thousandths) decimal place print("Round number \(n3):", round(n3 * 1000)/1000.0)

Output

Round number 34.459: 34.5
Round number 7.89098: 7.89
Round number -3.96396238638: -3.964

Method 2 - Using Ceil Function

Swift provide a built-in function named as ceil() function. This function is used to round the given number to the nearest smallest integer value which is greater than or equal to the given number. It accept also both Float and Double.

Syntax

Following is the syntax −

func ceil(num)

Example 1

The following program shows how to round a nu mber to n decimal places using ceil() function.

import Swift import Foundation var n1 : Float = 2.45 var n2 : Double = 42.93 var n3 : Double = -2.03 // Floating point number print("Round number \(n1):", ceil(n1)) // Positive double number print("Round number \(n2):", ceil(n2)) // Negative double number print("Round number \(n3):", ceil(n3))

Output

Round number 2.45: 3.0
Round number 42.93: 43.0
Round number
2.03: 2.0

Example 2

The following program shows how to round a number to n decimal places using ceil() function.

import Swift import Foundation var n1 : Float = 2.45 var n2 : Double = 198.890 var n3 : Double = -2.0808777 // Round Floating point number to one(tenths) decimal place print("Round number \(n1):", ceil(n1 * 10)/10.0) // Round positive double number to two(hundredths) decimal place print("Round number \(n2):", ceil(n2 * 100)/100.0) // Round negative double number to three(thousandths) decimal place print("Round number \(n3):", ceil(n3 * 1000)/1000.0)

Output

Round number 2.45: 2.5
Round number 198.89: 198.89
Round number -2.0808777: -2.08

Method 3 - Using Floor Function

Swift provide a built-in function named as floor() function. This function is used to round the given number to the nearest largest integer value which is less than or equal to the given number. It accept also both Float and Double.

Syntax

Following is the syntax

func floor(num)

Example 1

The following program shows how to round a number to n decimal places using floor() function.

import Swift import Foundation var n1 : Float = 23.45 var n2 : Double = 4.89 var n3 : Double = -7.09 // Floating point number print("Round number \(n1):", floor(n1)) // Positive double number print("Round number \(n2):", floor(n2)) // Negative double number print("Round number \(n3):", floor(n3))

Output

Round number 23.45: 23.0
Round number 4.89: 4.0
Round number -7.09: -8.0

Example 2

The following program shows how to round a number to n decimal places using floor() function.

import Swift import Foundation var n1 : Float = 256.45 var n2 : Double = 98.89324 var n3 : Double = -5.0808777 // Round Floating point number to one(tenths) decimal place print("Round number \(n1):", floor(n1 * 10)/10) // Round positive double number to two(hundredths) decimal place print("Round number \(n2):", floor(n2 * 100)/100) // Round negative double number to three(thousandths) decimal place print("Round number \(n3):", floor(n3 * 1000)/1000)

Output

Round number 256.45: 256.4
Round number 98.89324: 98.89
Round number -5.0808777: -5.081

Updated on: 18-Aug-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements