Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the methods that php float does not round to intercept floating-point strings

Shulou Source: shulou.com Published: 2022-06-03 07:39:47 10月01日 Update

This article mainly introduces "what are the methods of php float not rounding to intercept floating-point strings". In daily operations, I believe many people have doubts about the methods of php float not rounding and intercepting floating-point strings. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "php float does not round off to intercept floating-point strings." Next, please follow the editor to study!

There are roughly several ways to intercept floating-point types in php:

1. Float round (float $val [, int $precision]) returns the result of rounding val based on the specified precision precision (the number of 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 a string of formatted data

The copy code is as follows:

$axim 12.338938438

Echo sprintf ("% .5f", $a) / / result: 12.33894

$axim 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)

The copy code is as follows:

$number = 1234.5678

$english_format_number = number_format ($number, 2,'.'')

Echo $english_format_number; / / 1234.57

All of the above are automatically rounded, sometimes the demand does not need to be rounded, how to do, did not think of a good way, who knows can tell.

I wrote a trouble-point function and recorded it.

The copy code is as follows:

Function getFloatValue ($fjcjinlen)

{

$tmpInt=intval ($f)

$tmpDecimal=$f-$tmpInt

$str= "$tmpDecimal"

$subStr=strstr ($str,'.')

If (strlen ($subStr)

Tags: Rounding rounding method code character string floating point learning decimal decimal point more result help practical next function decimal what to do number data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information MySQL Apple Docker