PHP中的MySQL十进制值的类型

时间:2022-06-01 14:09:05

I am using BC Math library in PHP, as you know data provided to BC functions MUST BE strings, otherwise very weird results might come out.

我在PHP中使用BC Math库,因为您知道提供给BC函数的数据必须是字符串,否则可能会出现非常奇怪的结果。

I am storing decimal values (18,10) in MySQL.

我在MySQL中存储十进制值(18,10)。

My questions are:

我的问题是:

  1. What is the type of these values when i fetch them in PHP, are they string? and can i use them directly in BC such as bcmul($database_decimal_value , '1', '10') ?

    当我在PHP中获取它们时,这些值的类型是什么,它们是字符串吗?我可以直接在BC中使用它们,例如bcmul($ database_decimal_value,'1','10')?

  2. Can i use output of BC functions (which is string i guess) and insert in MySQL decimal type directly?

    我可以使用BC函数的输出(我猜的是字符串)并直接插入MySQL十进制类型吗?

P.S. i have read the MySQL reference and it says: Values for DECIMAL columns no longer are represented as strings that require 1 byte per digit or sign character. not sure if this means they are no longer strings.

附:我已阅读MySQL参考文件并说:DECIMAL列的值不再表示为每个数字或符号字符需要1个字节的字符串。不确定这是否意味着它们不再是字符串。

Edit:

I test the answer with the following code:

我用以下代码测试答案:

<?php
$var = 0.00001;
echo bcmul('10', "$var", '5');
?>

But it echoes 0 instead of 0.00010 ! if i change $var = 0.00001 to $var = '0.00001'; (which is string) it works

但它回应0而不是0.00010!如果我将$ var = 0.00001更改为$ var ='0.00001'; (这是字符串)它的工作原理

1 个解决方案

#1


Regardless of whether the value itself is a string or a number, you can force it to be passed as a string using proper quotes using strval()

无论值本身是字符串还是数字,您都可以使用strval()强制使用适当的引号将其作为字符串传递

bcmul(strval($database_decimal_value) , '1', '10');

And yes, you can pass those values to MySQL without quotes and it will treat them as numbers

是的,您可以将这些值传递给没有引号的MySQL,它会将它们视为数字

#1


Regardless of whether the value itself is a string or a number, you can force it to be passed as a string using proper quotes using strval()

无论值本身是字符串还是数字,您都可以使用strval()强制使用适当的引号将其作为字符串传递

bcmul(strval($database_decimal_value) , '1', '10');

And yes, you can pass those values to MySQL without quotes and it will treat them as numbers

是的,您可以将这些值传递给没有引号的MySQL,它会将它们视为数字