问题描述
这是对我在 mongodb 中插入整数的另一篇文章的后续.我通过使用 (int) 可以正常工作,但是现在当它将数字插入数据库时,会删除小数点后的所有内容.因此,如果我有 154.65,它将作为 154 插入到 mongodb 中,小数点后没有任何内容.
This is a follow up to another post i made about inserting an integer into mongodb. I have this working just fine by using the (int) but now when it inserts the number into the db is strips everything after the decimal. So if i had 154.65 it would be inserted into the mongodb as 154 with nothing after the decimal.
我的问题是我如何让这个保留小数点后的所有数字?此外,如果有人有这些 sting/numeric 函数的链接,我将不胜感激.谢谢.
My question is how do i get this to keep all numbers after the decimal? Also if anyone has a link to these sting/numeric functions i'd appreciate a reference. Thanks.
$number["xyz"] = (int) $_POST["number"] ;
$collection->insert($number);
这会将其作为整数插入,但会删除小数点后的所有内容.我试过你的建议
This inserts it as a integer but strips everything after the decimal. I tried you suggestion
$number["xyz"] = floatval($_POST["number"]) ;
$collection->insert($number);
但这仍然会删除小数点后的所有内容.我虽然我需要一些看起来像这样的东西,所以它是 mongo 格式:
but this still strips off everything after the decimal. My though it i need something that looks like this so that it is in mongo format:
$number["xyz"] = (dec) $_POST["number"] ;
$collection->insert($number);
推荐答案
没有原生支持的十进制格式.您的选项是浮点数(在保存之前转换)、整数(这会导致您看到小数点被删除)或字符串(在保存之前转换).最佳选择取决于您的功能要求.例如,如果它是一个货币值,您不想使用浮点数,而是将值以美分存储为整数(在您的示例中为 15465).
There is no natively supported decimal format. Your options are floating point (cast prior to saving it), integer (which results in the removal of decimals as you're seeing) or string (cast prior to saving it). The optimal choice depends on your functional requirements. For example, if it's a monetary value you do not want to use floats but store the value in cents as an integer (15465 in your example).
这篇关于Mongodb PHP - 带小数的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!