Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Round-off a number to a given number of significant digits
Next article icon

Round-off a number to a given number of significant digits

Last Updated : 16 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a positive number n (n > 1), round-off this number to a given no. of significant digits, d.
Examples: 
 

Input : n = 139.59          d = 4  Output : The number after rounding-off is 139.6 .    The number 139.59 has 5 significant figures and for rounding-off   the number to 4 significant figures, 139.59 is converted to 139.6 .    Input : n = 1240          d = 2  Output : The number after rounding-off is 1200 .


 

What are significant figures?


Each of the digits of a number that are used to express it to the required degree of accuracy, starting from the first non-zero digit, are called as significant figures.
Since there are numbers with large number of digits, for example, \frac{22}{7}    = 3.142857143, so in order to limit such numbers to a manageable number of digits, we drop unwanted digits and this process is called rounding off.
Significant digits include all the digits in a number falling in one of the following categories - 
 

  • All non-zero digits.
  • Zero digits which- 
    1. lie between significant digits.
    2. lie to the right of decimal point and at the same time to the right of a non-zero digit.
    3. are specifically indicated to be significant.


The following table shows numbers and no. of significant digits present in them -
 


 

Rules for Rounding-off a Number


To round off a number to n significant figures- 
 

  1. Discard all digits to the right of nth significant digit.
  2. If this discarded number is- 
    • less than half a unit in nthplace, leave the nth digit unchanged.
    • greater than half a unit in the nth place, increase the nth digit by unity.
    • exactly half a unit in the nth place, increase the nth digit by unity if its odd, otherwise leave it unchanged.


The following table shows rounding-off a number to a given no. of significant digits -
 


 

C++
// C++ program to round-off a number to given no. of // significant digits #include <bits/stdc++.h> using namespace std;  // Function to round - off the number void Round_off(double N, double n) {     int h;     double l, a, b, c, d, e, i, j, m, f, g;     b = N;     c = floor(N);      // Counting the no. of digits to the left of decimal point     // in the given no.     for (i = 0; b >= 1; ++i)         b = b / 10;      d = n - i;     b = N;     b = b * pow(10, d);     e = b + 0.5;     if ((float)e == (float)ceil(b)) {         f = (ceil(b));         h = f - 2;         if (h % 2 != 0) {             e = e - 1;         }     }     j = floor(e);     m = pow(10, d);     j = j / m;     cout << "The number after rounding-off is " << j; }  // Driver main function int main() {     double N, n;      // Number to be rounded - off     N = 139.59;      // No. of Significant digits required in the no.     n = 4;      Round_off(N, n);     return 0; } 
Java
// Java program to round-off a number to given no. of // significant digits  import java.io.*; import static java.lang.Math.*; public class A {      // Function to round - off the number     static void Round_off(double N, double n)     {         int h;         double l, a, b, c, d, e, i, j, m, f, g;         b = N;         c = floor(N);          // Counting the no. of digits to the left of decimal point         // in the given no.         for (i = 0; b >= 1; ++i)             b = b / 10;          d = n - i;         b = N;         b = b * pow(10, d);         e = b + 0.5;         if ((float)e == (float)ceil(b)) {             f = (ceil(b));             h = (int)(f - 2);             if (h % 2 != 0) {                 e = e - 1;             }         }         j = floor(e);         m = pow(10, d);         j = j / m;         System.out.println("The number after rounding-off is "                            + j);     }      // Driver main function     public static void main(String args[])     {         double N, n;          // Number to be rounded - off         N = 139.59;          // No. of Significant digits required in the no.         n = 4;          Round_off(N, n);     } } 
Python3
# Python 3 program to round-off a number  # to given no. of significant digits from math import ceil, floor, pow  # Function to round - off the number def Round_off(N, n):     b = N     c = floor(N)      # Counting the no. of digits      # to the left of decimal point      # in the given no.     i = 0;     while(b >= 1):         b = b / 10         i = i + 1      d = n - i     b = N     b = b * pow(10, d)     e = b + 0.5     if (float(e) == float(ceil(b))):         f = (ceil(b))         h = f - 2         if (h % 2 != 0):             e = e - 1     j = floor(e)     m = pow(10, d)     j = j / m     print("The number after rounding-off is", j)  # Driver Code if __name__ == '__main__':          # Number to be rounded - off     N = 139.59      # No. of Significant digits      # required in the no.     n = 4      Round_off(N, n)  # This code is contributed by # Surendra_Gangwar 
C#
// C# program to round-off a number  // to given no. of significant digits using System;  class A {      // Function to round - off the number     static void Round_off(double N, double n)     {         int h;         double b, d, e, i, j, m, f;         b = N;         // c = Math.Floor(N);          // Counting the no. of digits to the         // left of decimal point in the given no.         for (i = 0; b >= 1; ++i)             b = b / 10;          d = n - i;         b = N;         b = b * Math.Pow(10, d);         e = b + 0.5;         if ((float)e == (float)Math.Ceiling(b)) {             f = (Math.Ceiling(b));             h = (int)(f - 2);             if (h % 2 != 0) {                 e = e - 1;             }         }         j = Math.Floor(e);         m = Math.Pow(10, d);         j = j / m;         Console.WriteLine("The number after " +                         "rounding-off is " + j);     }      // Driver main function     public static void Main()     {         double N, n;          // Number to be rounded - off         N = 139.59;          // No. of Significant digits required in the no.         n = 4;          Round_off(N, n);     } }  // This code is contributed by vt_m. 
PHP
<?php // PHP program to round-off // a number to given no. of // significant digits  // Function to round -  // off the number function Round_off($N, $n) {          $h;     $l; $a; $b; $c;      $d; $e; $i; $j;      $m; $f; $g;     $b = $N;     $c = floor($N);      // Counting the no. of digits      // to the left of decimal point     // in the given no.     for ($i = 0; $b >= 1; ++$i)         $b = $b / 10;      $d = $n - $i;     $b = $N;     $b = $b * pow(10, $d);     $e = $b + 0.5;     if ($e == ceil($b))      {         $f = (ceil($b));         $h = $f - 2;         if ($h % 2 != 0)          {             $e = $e - 1;         }     }     $j = floor($e);     $m = pow(10, $d);     $j = $j / $m;     echo "The number after rounding-off is " ,$j; }      // Driver Code     $N; $n;      // Number to be rounded - off     $N = 139.59;      // No. of Significant digits     // required in the no.     $n = 4;      Round_off($N, $n);  // This code is contributed by anuj_67 ?> 
JavaScript
<script>  // Javascript program to round-off a number to given no. of // significant digitsimport   // Function to round - off the number function Round_off(N , n) {     var h;     var l, a, b, c, d, e, i, j, m, f, g;     b = N;     c = Math.floor(N);      // Counting the no. of digits to the left of decimal point     // in the given no.     for (i = 0; b >= 1; ++i)         b = parseInt(b / 10);      d = n - i;     b = N;     b = b * Math.pow(10, d);     e = b + 0.5;     if (e == Math.ceil(b)) {         f = (Math.ceil(b));         h = parseInt(f - 2);         if (h % 2 != 0) {             e = e - 1;         }     }     j = Math.floor(e);     m = Math.pow(10, d);     j = j / m;     document.write("The number after rounding-off is "                        + j); }  // Driver main function var N, n;  // Number to be rounded - off N = 139.59;  // No. of Significant digits required in the no. n = 4;  Round_off(N, n);  // This code contributed by Princi Singh   </script> 

Output: 

The number after rounding-off is 139.6 

Time complexity: O(logN)

Space complexity: O(1) 


Next Article
Round-off a number to a given number of significant digits

K

kartik
Improve
Article Tags :
  • Mathematical
  • DSA
  • Basic Coding Problems
Practice Tags :
  • Mathematical

Similar Reads

    Round the given number to nearest multiple of 10 | Set-2
    Given a large positive integer represented as a string str. The task is to round this number to the nearest multiple of 10. Examples: Input: str = "99999999999999993"Output: 99999999999999990 Input: str = "99999999999999996" Output: 100000000000000000 Approach: A solution to the same problem has bee
    10 min read
    Round the given number to nearest multiple of 10
    Given a positive integer n, round it to nearest whole number having zero as last digit. Examples: Input : 4722Output : 4720Input : 38Output : 40Input : 10Output: 10Recommended PracticeNearest multiple of 10Try It!Approach: Let's round down the given number n to the nearest integer which ends with 0
    8 min read
    JavaScript Number toPrecision() Method
    The JavaScript Number toPrecision( ) method in Javascript is used to format a number to a specific precision or length. If the formatted number requires more digits than the original number then decimals and nulls are also added to create the specified length. Syntax: number.toPrecision(value) Param
    2 min read
    JavaScript Number toFixed() Method
    The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals. Notes:The toFixed() method rounds the number if necessary.If the specified number of digits is greater than the actual number of digits after the decimal point, zeros are add
    2 min read
    JavaScript Math round() Method
    The JavaScript Math.round() method rounds a number to the nearest integer. It rounds up for decimal values of 0.5 and higher, and down otherwise.Syntax:Math.round(value);Parameters:value: It is the number that you want to round off.Return Value:The Math.round() method returns the value of the given
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences