Kotlin Program to Round a Number to n Decimal Places

In this article, we will understand how to round a number to n decimal places. Rounding of decimal values are done using the ceil() or floor() functions.

Below is a demonstration of the same ?

Suppose our input is

Input : 3.1415

The desired output would be ?

Output : 3.2

Algorithm

  • Step 1 ? START

  • Step 2 ? Declare a float value namely myInput.

  • Step 3 ? Define the values

  • Step 4 ? Use format() to alter the number of decimal places required. Store the result.

  • Step 5 ? Display the result

  • Step 6 ? Stop

Example 1

In this example, we will Round a Number to n Decimal Places. First, let us declare a variable for input

val myInput = 3.1415

Now, we will format and round the input to 3 decimal places using %.3f ?

println("%.3f".format(myInput))

Let us now see the complete example to round a number to n decimal places ?

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> myInput <span class="token operator">=</span> <span class="token number">3.1415</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The number is defined as <span class="token interpolation variable">$myInput</span>"</span><span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The result after rounding the number to 3 decimal places is: "</span><span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"%.3f"</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>myInput<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

The number is defined as 3.1415
The result after rounding the number to 3 decimal places is: 3.142

Example 2

In this example, we will round a Number to n Decimal Places ?

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> myInput <span class="token operator">=</span> <span class="token number">3.1415</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The number is defined as <span class="token interpolation variable">$myInput</span>"</span><span class="token punctuation">)</span>
   <span class="token function">roundDecimal</span><span class="token punctuation">(</span>myInput<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">fun</span> <span class="token function">roundDecimal</span><span class="token punctuation">(</span>myInput<span class="token operator">:</span> Double<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The result after rounding the number to 3 decimal places is: "</span><span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"%.3f"</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>myInput<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

The number is defined as 3.1415
The result after rounding the number to 3 decimal places is: 3.142
Updated on: 2022-10-13T13:38:07+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements