Home > Backend Development > PHP Problem > What is the usage of bcsub in php

What is the usage of bcsub in php

WBOY
Release: 2023-03-15 16:22:01
Original
5842 people have browsed it

In PHP, the bcsub() function is used to subtract an arbitrary precision number from another function that accepts two arbitrary precision numbers as strings and scales the result to the specified precision. Then return the subtraction of two numbers, the syntax is "bcsub($num_str1,$num_str2,$scaleVal)".

What is the usage of bcsub in php

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What is the usage of bcsub in php

The bcsub() function in PHP is a built-in function used to subtract an arbitrary precision number from another function. This function accepts two arbitrary-precision numbers as strings and returns the subtraction of the two numbers after scaling the result to the specified precision.

Usage:

string bcsub ( $num_str1, $num_str2, $scaleVal)
Copy after login

Parameters: This function accepts three parameters as shown in the above syntax and explained below:

$num_str1: This parameter is a string Type that represents the left operand or one of the two numbers we want to subtract. This parameter is required.

$num_str2: This parameter is of type string and represents the right operand or one of the two numbers we want to subtract. This parameter is required.

$scaleVal: This parameter is of type int and is optional. This parameter tells the number of digits that will appear after the decimal point in the addition result. The default value is zero.

Return value: This function returns the subtraction of two numbers $num_str1 and $num_str2 in string form.

Example:

Input:  $num_str1 = 11.222, $num_str2 = 3
Output: 14
Since the parameter $scaleVal is not specified so
no digits after decimal is appeared in the 
result after subtraction.
Input:  $num_str1 = 11.222, $num_str2 = 3, $scaleVal = 4
Output: 8.2220
Copy after login

The following example program is intended to illustrate the bcsub() function in PHP:

Program 1:

<?php 
// PHP program to illustrate bcsub() function 
   
// input numbers with arbitrary precision 
$num_str1 = "11.222"; 
$num_str2 = "3"; 
   
// calculates the subtraction of 
// the two numbers when $scaleVal is 
// not specified 
$res = bcsub($num_str1, $num_str2); 
  
echo $res; 
   
?>
Copy after login

Output:

8

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the usage of bcsub in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template