使用php添加货币值

时间:2017-01-22 09:26:05

标签: php

我尝试使用此代码添加格式化货币的值但是当我尝试添加它给我不同的值时。

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        WebView webView = (WebView) v; 
        HitTestResult hr = webView.getHitTestResult(); 

         //Setting Hit-Tests to go for Images Only!
        int type = hr.getType();

        String  imageUrl = hr.getExtra();

        if (type == HitTestResult.IMAGE_TYPE || type == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {


                menu.setHeaderTitle("Context Menu");
        menu.add(0, v.getId(), 0, "Save");

}
    }


    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        if(item.getTitle()== "Save")
            {   
                //File & Folder Structure on your SdCard to Save Images
                File file = new File
                (Environment.getExternalStorageDirectory() + "/XYZ/"); 
                if (!file.isDirectory()) 
                {
                    file.mkdirs(); //If folder doesn't exist, this will Create One! :)
                }



                //Final Output will be "Your App Folder/Image name.jpg"
                File savePic = new File (file, getFilenameFromURL(imageUrl));


                //Setting up Download Manager to Show Image Download on Notif Panel/Status Bar :)
                DownloadManager downloadManager = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imageUrl));
                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
                request.setDestinationUri(Uri.fromFile(savePic));
                request.setTitle("XYZ"); //Title Visible during Image Download
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                downloadManager.enqueue(request);

            Toast.makeText(this,"Action 1 invoked", Toast.LENGTH_SHORT).show();
            }
            return true;
            }



    protected String getFilenameFromURL(URL url) {
        return getFilenameFromURL(url.getFile());
    }

    protected String getFilenameFromURL(String url) {
        String[] p = url.split("/");
        String s = p[p.length - 1];
        if (s.indexOf("?") > -1) {
            return s.substring(0, s.indexOf("?"));
        }
        return s;
    }

输出:51.00

预期产出:51,000.00

1 个答案:

答案 0 :(得分:1)

number_format函数要求第一个参数为float,但由于值中有逗号,因此php无法定义数字的小数部分。首先,您需要删除逗号,然后将字符串转换为float类型。

<?php
$item1="50,000.00";
$item2="1,000.00";

$itemFloat1 = floatval(str_replace(",", "", $item1));
$itemFloat2 = floatval(str_replace(",", "", $item2));

echo $total= number_format($itemFloat1 + $itemFloat2, 2);