我正在尝试获取包含数字的变量总数,有些可能是小数。我需要保留两位小数并使用 number_format() 函数。
$total = $order->order->net+$order->order->deductions+$order->order->vat+$order->order->postage+$order->order->postage_tax;
echo number_format((float)$total, 2, '.', '');?>
我注意到以下值加起来不正确,似乎忽略了小数点。总数应该是 118.50,但我得到的是 118.00。
100+0+17.5+1+0
我研究了这个并发现了以下内容
http://floating-point-gui.de/basic/
我有点困惑。谁能解释我需要做什么?
*编辑 下面是 $order 变量的转储,显示了我要加起来的数字。你可以看到 17.5 是 17.5 而不是 17。可能是因为它们被指定为字符串?
object(SimpleXMLElement)#12 (21) { ["id"]=> string(6) "922704"["shopkeeper_orderno"]=> string(4) "1001"["customer"]= > string(6) "797893"["creationdate"]=> string(16) "29-05-2012 11:55"["net"]=> string(3) "100"["vat"]=> string(4) "17.5"["status"]=> string(1) "1"["isnew"]=> string(1) "0"["deductions"]=> string(1) "0"[ "postage"]=> string(1) "1"["paymentmethod"]=> string(20) "PayPal "["instructions"]=> object(SimpleXMLElement)#17 (0) { } [2]=> object(SimpleXMLElement)#22 (1) { ["items"]=> object(SimpleXMLElement)#30 (9) { ["id"]=> string(7) "1384486"["headerID"]=> string( 6) "922704"["productID"]=> string(7) "4959678"["description"]=> string(13) "婚纱"["net"]=> string(3) "100"["vat"]=> string(4) "17.5"["qty"]=> string(1) "1"["formID"]=> string(2) "-1"["options"]=> object( SimpleXMLElement)#31 (1) { ["options"]=> array(2) { [0]=> object(SimpleXMLElement)#32 (6) { ["id"]=> string(6) "519981"[ “orderDetailsID”]=> string(7) “1384486” [“optionid”]=> string(6) "646934"["optionCost"]=> string(1) "0"["optionVAT"]=> string(1) "0"["customText"]=> string(9) "size : 12"} [1 ]=> object(SimpleXMLElement)#33 (6) { ["id"]=> string(6) "519982"["orderDetailsID"]=> string(7) "1384486"["optionid"]=> string( 6) "647285"["optionCost"]=> string(1) "0"["optionVAT"]=> string(1) "0"["customText"]=> string(14) "Colour : Ivory"} } } } } } ["postage_tax"]=> string(1) "0"["dispatched"]=> string(1) "0"["paybyotherid"]=> string(2) "-1"["wheredidyouhearid"]=> string(2) "-1" }
最佳答案
您可以使用先轮后数的格式:
$total = 100+0+17.5+1+0.2;
//echo number_format((float)$total); //119
echo number_format(round((float)$total,2),2); //118.50
关于PHP float 未正确添加 - 缺少十进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11243678/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参
我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca