📜  将数字四舍五入为给定数量的有效数字

📅  最后修改于: 2021-05-04 19:12:43             🧑  作者: Mango

给定正数n(n> 1),请将此数字四舍五入为给定的数字。有效位数d。
例子:

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 .

什么是重要数字?

从第一个非零数字开始,用于将数字表示为所需精度的数字中的每个数字都称为有效数字。
例如,由于存在具有大量数字的数字, \frac{22}{7}  = 3.142857143,因此为了将此类数字限制为可管理的数字,我们删除了不需要的数字,此过程称为舍入
有效数字包括属于以下类别之一的数字中的所有数字–

  • 所有非零数字。
  • 零位数字-
    1. 位于有效数字之间。
    2. 位于小数点右边,同时位于非零数字右边。
    3. 特别指出是有意义的。

下表显示编号和编号。其中存在的有效数字–

四舍五入规则

将数字四舍五入为n个有效数字-

  1. 舍弃n有效数字右边的所有数字。
  2. 如果这个被遗弃的号码是-
    • n位中少于半个单位的情况下,请保持第n数字不变。
    • 大于在n位置的一半的单元,通过统一增加n位。
    • 在第n恰好半个单位,如果n位为奇数,则将其增加1 ,否则保持不变。

下表显示了将数字四舍五入为给定的数字。有效位数–

C++
// C++ program to round-off a number to given no. of
// significant digits
#include 
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
= 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


输出:

The number after rounding-off is 139.6