簡體   English   中英

我想減去兩個貨幣數字

[英]I want to subtract two currency numbers

我想減去兩個數字,但它們都有貨幣。 (25 美元-10 美元)

<?php
    $tamount= $agreementdata['amount'];
    $eamount= $agreementdata['earnestamount'];
    $ramount= $agreementdata['amount'-'earnestamount'];  
    echo $ramount;
?> 
  $currencyFrom = '25$';
  $currency = '20$';
  $results = (int)substr($currencyFrom, 0, -1) - (int)substr($currency, 0, -1);
  echo $results;

我希望這會回答你的問題。 如果您同意解決方案和想法,請標記為已接受。

https://www.php.net/manual/en/language.types.integer.php

轉換為整數:從字符串

如果字符串是數字或前導數字,則它將解析為相應的整數值,否則將轉換為零 (0)。

所以(int)'25$'將返回25 ,並重新添加美元符號,以便數組可以理解它。

echo (int)'25$'-(int)'20$'.'$';
<?php
    $tamount= $agreementdata['amount'];
    $eamount= $agreementdata['earnestamount'];
    $ramount=  $tamount - $eamount;

    echo $ramount;
?>

如果您想要更大的靈活性,我會使用正則表達式。

$str = '25$ 10Rupiah 25 $ 10 Rupiah';
$pattern = '/[A-Z|\$]*/i'; 
echo preg_replace($pattern, '', $str);

// output 25 10 25  10 

這將尋找您的案例:

<?php
    function getAmount($str) {
       $pattern = '/[A-Z|\$]*/i'; 
       return (int) preg_replace($pattern, '', $str);
    }

    $tamount= getAmount( $agreementdata['amount'] );
    $eamount= getAmount( $agreementdata['earnestamount'] );
    $ramount= $agreementdata['amount'-'earnestamount'];  
    echo $ramount;
?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM