dongtiaozhou4914 2016-03-17 15:48
浏览 18
已采纳

将金钱串加在一起并获得总额

I have just coded a basket/cart system for my websitye but I am sturggling on getting the total cost for items in the basket? I have the price for each item (i loop basket items in a loop) what the main question here is how can I add money strings together and get a valid money string total amount like below...

£4.56 + £2.35 + £3.00 = £9.91

£2.83 + £19.83 + £22 = £44.66 (I think)

  • 写回答

2条回答 默认 最新

  • dtrb96410 2016-03-17 16:03
    关注

    you need to remove tha '£' from each item and then convert it to a integer then you create a variable named sum and in a loop you add each element to the sum , there you are the algorithm :

     int sum = 0;
       for_each (item i in basket)
        {
            string tmp = i.get_price() ; // get_price returns "£x" string
             sum += to_integer(tmp.sub_str(1,tmp.length()-1)); 
       /* copy from the pos 1 to the end ignoring the '£'*/
        }
    

    then the sum variable contains what you want :D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题