[英]I want to subtract two currency numbers
I want to subtract two numbers but both of them have currency.我想减去两个数字,但它们都有货币。 (25$-10$)
(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;
I hope this will answer your question.我希望这会回答你的问题。 Please mark it accepted if you are agree with the solution and idea.
如果您同意解决方案和想法,请标记为已接受。
From从
https://www.php.net/manual/en/language.types.integer.php
https://www.php.net/manual/en/language.types.integer.php
Converting to integer : From strings转换为整数:从字符串
If the string is numeric or leading numeric then it will resolve to the corresponding integer value, otherwise it is converted to zero (0).
如果字符串是数字或前导数字,则它将解析为相应的整数值,否则将转换为零 (0)。
So (int)'25$'
will return 25
, and add the dollar sign back on so that it can be understood by the array.所以
(int)'25$'
将返回25
,并重新添加美元符号,以便数组可以理解它。
echo (int)'25$'-(int)'20$'.'$';
<?php
$tamount= $agreementdata['amount'];
$eamount= $agreementdata['earnestamount'];
$ramount= $tamount - $eamount;
echo $ramount;
?>
If you want more flexibility, I would work with regex.如果您想要更大的灵活性,我会使用正则表达式。
$str = '25$ 10Rupiah 25 $ 10 Rupiah';
$pattern = '/[A-Z|\$]*/i';
echo preg_replace($pattern, '', $str);
// output 25 10 25 10
That will look for your case:这将寻找您的案例:
<?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.