Hướng dẫn dùng truncate math trong PHP

Trong bài này mình sẽ hướng dẫn cách sử dụng hàm TRUNCATE trong MySQL thông qua cú pháp và ví dụ thực tế.

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Mô tả

Hàm TRUNCATE trả về một số bị cắt cụt đến một số vị trí thập phân nhất định.

2. Cú pháp

Cú pháp của hàm TRUNCATE trong MySQL là:

TRUNCATE[ number, decimal_places ]

Trong đó:

Bài viết này được đăng tại [free tuts .net]

  • number:  Số để cắt ngắn
  • decimal_places: Số lượng các vị trí thập phân cắt ngắn đến. Giá trị này phải là số nguyên dương hoặc âm.

3. Version

Hàm TRUNCATE có thể được sử dụng trong các phiên bản sau của MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

4. Ví dụ

mysql> SELECT TRUNCATE[115.34523, 0];
Ket qua: 115

mysql> SELECT TRUNCATE[115.34523, 1];
Ket qua: 115.3

mysql> SELECT TRUNCATE[115.34523, 2];
Ket qua: 115.34

mysql> SELECT TRUNCATE[115.34523, -1];
Ket qua: 110

mysql> SELECT TRUNCATE[115.34523, -2];
Ket qua: 100

mysql> SELECT TRUNCATE[-115.34523, 0];
Ket qua: -115

Bitwise operators allow evaluation and manipulation of specific bits within an integer.

Bitwise OperatorsExampleNameResult
$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or [inclusive or] Bits that are set in either $a or $b are set.
$a ^ $b Xor [exclusive or] Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a > $b Shift right Shift the bits of $a $b steps to the right [each step means "divide by two"]

Bit shifting in PHP is arithmetic. Bits shifted off either end are discarded. Left shifts have zeros shifted in on the right while the sign bit is shifted out on the left, meaning the sign of an operand is not preserved. Right shifts have copies of the sign bit shifted in on the left, meaning the sign of an operand is preserved.

Use parentheses to ensure the desired precedence. For example, $a & $b == true evaluates the equivalency then the bitwise and; while [$a & $b] == true evaluates the bitwise and then the equivalency.

If both operands for the &, | and ^ operators are strings, then the operation will be performed on the ASCII values of the characters that make up the strings and the result will be a string. In all other cases, both operands will be converted to integers and the result will be an integer.

If the operand for the ~ operator is a string, the operation will be performed on the ASCII values of the characters that make up the string and the result will be a string, otherwise the operand and the result will be treated as integers.

Both operands and the result for the operators are always treated as integers.

PHP's error_reporting ini setting uses bitwise values,
providing a real-world demonstration of turning
bits off. To show all errors, except for notices,
the php.ini file instructions say to use:
E_ALL & ~E_NOTICE
      
This works by starting with E_ALL:
00000000000000000111011111111111
Then taking the value of E_NOTICE...
00000000000000000000000000001000
... and inverting it via ~:
11111111111111111111111111110111
Finally, it uses AND [&] to find the bits turned
on in both values:
00000000000000000111011111110111
      
Another way to accomplish that is using XOR [^]
to find bits that are on in only one value or the other:
E_ALL ^ E_NOTICE
      

error_reporting can also be used to demonstrate turning bits on.
The way to show just errors and recoverable errors is:
E_ERROR | E_RECOVERABLE_ERROR
      
This process combines E_ERROR
00000000000000000000000000000001
and
00000000000000000001000000000000
using the OR [|] operator
to get the bits turned on in either value:
00000000000000000001000000000001
      

Example #1 Bitwise AND, OR and XOR operations on integers

The above example will output:

 ---------     ---------  -- ---------
 result        value      op test
 ---------     ---------  -- ---------
 Bitwise AND
[ 0 = 0000] = [ 0 = 0000] & [ 5 = 0101]
[ 1 = 0001] = [ 1 = 0001] & [ 5 = 0101]
[ 0 = 0000] = [ 2 = 0010] & [ 5 = 0101]
[ 4 = 0100] = [ 4 = 0100] & [ 5 = 0101]
[ 0 = 0000] = [ 8 = 1000] & [ 5 = 0101]

 Bitwise Inclusive OR
[ 5 = 0101] = [ 0 = 0000] | [ 5 = 0101]
[ 5 = 0101] = [ 1 = 0001] | [ 5 = 0101]
[ 7 = 0111] = [ 2 = 0010] | [ 5 = 0101]
[ 5 = 0101] = [ 4 = 0100] | [ 5 = 0101]
[13 = 1101] = [ 8 = 1000] | [ 5 = 0101]

 Bitwise Exclusive OR [XOR]
[ 5 = 0101] = [ 0 = 0000] ^ [ 5 = 0101]
[ 4 = 0100] = [ 1 = 0001] ^ [ 5 = 0101]
[ 7 = 0111] = [ 2 = 0010] ^ [ 5 = 0101]
[ 1 = 0001] = [ 4 = 0100] ^ [ 5 = 0101]
[13 = 1101] = [ 8 = 1000] ^ [ 5 = 0101]

Example #2 Bitwise XOR operations on strings

Example #3 Bit shifting on integers

Output of the above example on 32 bit machines:

--- BIT SHIFT RIGHT ON POSITIVE INTEGERS ---
Expression: 2 = 4 >> 1
 Decimal:
  val=4
  res=2
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000010
 NOTE: copy of sign bit shifted into left side

Expression: 1 = 4 >> 2
 Decimal:
  val=4
  res=1
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000001

Expression: 0 = 4 >> 3
 Decimal:
  val=4
  res=0
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000000
 NOTE: bits shift out right side

Expression: 0 = 4 >> 4
 Decimal:
  val=4
  res=0
 Binary:
  val=00000000000000000000000000000100
  res=00000000000000000000000000000000
 NOTE: same result as above; can not shift beyond 0


--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
Expression: -2 = -4 >> 1
 Decimal:
  val=-4
  res=-2
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111110
 NOTE: copy of sign bit shifted into left side

Expression: -1 = -4 >> 2
 Decimal:
  val=-4
  res=-1
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111111
 NOTE: bits shift out right side

Expression: -1 = -4 >> 3
 Decimal:
  val=-4
  res=-1
 Binary:
  val=11111111111111111111111111111100
  res=11111111111111111111111111111111
 NOTE: same result as above; can not shift beyond -1


--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
Expression: 8 = 4 > 4
 Decimal:
  val=4
  res=0
 Binary:
  val=0000000000000000000000000000000000000000000000000000000000000100
  res=0000000000000000000000000000000000000000000000000000000000000000
 NOTE: same result as above; can not shift beyond 0


--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---
Expression: -2 = -4 >> 1
 Decimal:
  val=-4
  res=-2
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111110
 NOTE: copy of sign bit shifted into left side

Expression: -1 = -4 >> 2
 Decimal:
  val=-4
  res=-1
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111111
 NOTE: bits shift out right side

Expression: -1 = -4 >> 3
 Decimal:
  val=-4
  res=-1
 Binary:
  val=1111111111111111111111111111111111111111111111111111111111111100
  res=1111111111111111111111111111111111111111111111111111111111111111
 NOTE: same result as above; can not shift beyond -1


--- BIT SHIFT LEFT ON POSITIVE INTEGERS ---
Expression: 8 = 4 

Chủ Đề