Home Backend Development PHP Tutorial Summary of methods for intercepting floating-point strings without rounding in php float_PHP Tutorial

Summary of methods for intercepting floating-point strings without rounding in php float_PHP Tutorial

Jul 13, 2016 am 10:25 AM
float rounding

There are roughly the following methods for intercepting floating point types in php:

1, float round ( float $val [, int $precision ] ) Returns val rounded to the specified precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default).

echo round(4.3) //4

2, string sprintf ( string $format [, mixed $args [, mixed $... ]] ) returns the string of formatted data

Copy code The code is as follows:

$a=12.338938438;
echo sprintf("%.5f",$a) //Result: 12.33894

$a=12.3312356;
echo sprintf("%.5f",$a);//12.33124
echo sprintf("%f",$a);//331236 Default 6 decimal places

3, string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )
Copy code The code is as follows:

$number = 1234.5678;

$english_format_number = number_format($number, 2, '.', '');
echo $english_format_number; // 1234.57

The above are automatically rounded. Sometimes rounding is not required. What should I do? I haven’t thought of a good way. Who knows can tell me.

I wrote a troublesome function myself and recorded it

Copy the code The code is as follows:

function getFloatValue($ f,$len)
{
$tmpInt=intval($f);

$tmpDecimal=$f-$tmpInt;
$str="$tmpDecimal";
$subStr=strstr($str,'.');
if(strlen($subStr)<$len+1)
{
$repeatCount=$len+1-strlen($subStr) ;
$str=$str."".str_repeat("0",$repeatCount);

}

return $tmpInt."".substr($str,1, 1+$len);

}
echo getFloatValue(12.99,4) //12.9900
echo getFloatValue(12.9232555553239,4) //12.9232

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824906.htmlTechArticleThere are roughly the following methods to intercept floating point types in PHP: 1. float round (float $val [, int $precision ] ) returns the value of val according to the specified precision (the number of decimal digits after the decimal point...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the maximum value of float? What is the maximum value of float? Oct 11, 2023 pm 05:54 PM

Maximum value of float: 1. In C language, the maximum value of float is 3.40282347e+38. According to the IEEE 754 standard, the maximum exponent of the float type is 127, and the number of digits of the mantissa is 23. In this way, the maximum floating point number is 3.40282347 e+38; 2. In the Java language, the maximum float value is 3.4028235E+38; 3. In the Python language, the maximum float value is 1.7976931348623157e+308.

How to round a number using Math.round() method in Java? How to round a number using Math.round() method in Java? Nov 18, 2023 pm 01:11 PM

How to round a number using Math.round() method in Java? Overview: The Math class in Java provides a series of convenient mathematical calculation methods, including a method called round(), which is used to round floating-point numbers. This article explains how to correctly use the Math.round() method to round numbers and provides corresponding code examples. Specific steps: Import the Math class: At the beginning of the code, you need to import java.la

PHP floating point number rounding method PHP floating point number rounding method Mar 21, 2024 am 09:21 AM

This article will explain in detail the PHP floating point number rounding method. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Floating Point Rounding Overview Floating point numbers are represented in computers as a decimal point followed by an exponent, however, they are often stored in approximations with a limited number of digits. When you need to round a floating point number to a specific precision, there are several ways to do it. Method 1. round() function The round() function rounds a floating point number to the nearest integer. It accepts floating point numbers and optional precision parameters. For example: $num=1.55;echoround($num);//Output: 2echoround($num,1)

C++ program to round a number to n decimal places C++ program to round a number to n decimal places Sep 12, 2023 pm 05:13 PM

Representing numbers as output is an interesting and important task when writing a program in any language. For integer types (data of type short, long, or medium), it is easy to represent numbers as output. For floating point numbers (float or double type), sometimes we need to round them to a specific number of decimal places. For example, if we want to represent 52.24568 as three decimal places, some preprocessing is required. In this article, we will introduce several techniques to represent floating point numbers to a specific number of decimal places by rounding. Among the different approaches, it is important to use a C-like format string, use the precision argument, and use the round() function from the math library. Let’s look at them one by one. with

js rounding js rounding Jul 04, 2023 am 10:07 AM

js rounding methods: 1. tofixed method, which can round Number to a number with specified decimal places; 2. round() method, which can round a number to the nearest integer.

What is the accuracy of float? What is the accuracy of float? Oct 17, 2023 pm 03:13 PM

The precision of float can reach 6 to 9 decimal places. According to the IEEE754 standard, the number of significant digits that the float type can represent is approximately 6 to 9 digits. It should be noted that this is only the theoretical maximum precision. In actual use, due to the rounding error of floating point numbers, the precision of the float type is often lower. When performing floating-point number operations in a computer, precision loss may occur due to the precision limitations of floating-point numbers. In order to improve the precision of floating point numbers, you can use higher precision data types, such as double or long double.

Round floating point numbers using Math.Round function in C# Round floating point numbers using Math.Round function in C# Nov 18, 2023 pm 02:17 PM

Using the Math.Round function in C# to round floating-point numbers requires specific code examples. In the C# programming language, sometimes we need to round floating-point numbers. At this time, we can use the Math.Round function to achieve this function. The Math.Round function is a built-in function in C# used for mathematical calculations. Its main function is to round the specified floating point number. The following is the common format of the Math.Round function: Math.Round(doub

What does float mean in c language? What does float mean in c language? Oct 12, 2023 pm 02:30 PM

Float in C language is a data type used to represent single-precision floating point numbers. Floating point numbers are real numbers represented in scientific notation and can represent very large or very small values. Variables of the float type can store values ​​with 6 significant digits after the decimal point. In C language, the float type can be used to operate and store floating point numbers. Its variables can be used to represent decimals, fractions, scientific notation, etc. that need to be accurately represented. Real numbers, unlike integer types, floating point numbers can represent numbers after the decimal point, and can perform four arithmetic operations on decimals.

See all articles