Mongodb PHP-带小数的整数 [英] Mongodb PHP - Integers with decimals

查看:84
本文介绍了Mongodb PHP-带小数的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我发表的另一篇有关将整数插入mongodb的文章的后续文章.我通过使用(int)可以正常工作,但是现在当它向db中插入数字时,会去除小数点后的所有内容.因此,如果我有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.

我的问题是我如何才能将所有数字保留在小数点后?另外,如果有人链接到这些字符串/数字函数,我将不胜感激.谢谢.

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-带小数的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆