Home Backend Development PHP Tutorial php float method to intercept floating point string without rounding

php float method to intercept floating point string without rounding

Jul 03, 2018 pm 05:21 PM
float rounding

There are roughly the following methods to intercept floating point types in php. Friends in need can come here for reference, I hope it will be helpful to everyone

There are roughly the following methods to intercept floating point types in php:

1, float round ( float $val [, int $precision ] ) returns the result of rounding val according 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 $... ]] ) return String of formatted data

$a=12.338938438; 
echo sprintf("%.5f",$a) //结果:12.33894  
$a=12.3312356; 
echo sprintf("%.5f",$a);//12.33124 
echo sprintf("%f",$a);//331236  默认小数点后6位
Copy after login

3, string number_format (float $number, int $decimals, string $dec_point, string $thousands_sep)

$number = 1234.5678; 
$english_format_number = number_format($number, 2, '.', ''); 
echo  $english_format_number ; // 1234.57
Copy after login

All of the above Automatic rounding is done. 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 and recorded it

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
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. More related content Please pay attention to PHP Chinese website!

Related recommendations:

Introduction to php using parse_url and parse_str to parse URLs

##PHP full-featured non-deformation image cropping Introduction to operation classes and usage

The above is the detailed content of php float method to intercept floating point string without rounding. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

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)

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.

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 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.

What does float32 bytes include? What does float32 bytes include? Oct 10, 2023 pm 04:07 PM

The float32 byte includes the sign bit, exponent bit and mantissa bit, and is used to represent 32-bit floating point numbers. Detailed introduction: 1. Sign bit (1 bit), used to represent the sign of a number, 0 represents a positive number, 1 represents a negative number; 2. Exponent bit (8 bits), used to represent the exponent part of a floating point number, through the exponent bit , you can adjust the size range of the floating-point number; 3. The mantissa bit (23 bits) is used to represent the mantissa part of the floating-point number, and the mantissa bit stores the decimal part of the floating-point number. The sign bit determines the sign of a floating point number, and the exponent bit and the mantissa bit jointly determine the size and precision of the floating point number.

See all articles