基本上我有以下字符串:
1,254.40
我必须将其转换为以下内容:
one * two * five * four * usd * four * zero * cents
我该怎么做?到目前为止,我写过这样的话:
public function priceToString($price)
{
$output = "";
$chars = str_split($price);
foreach($chars as $char)
{
if(is_numeric($char))
{
$output .= (string)$char . " *";
}
}
return $output;
}
但它不起作用,因为它只显示整数...我该如何解决这个问题?
答案 0 :(得分:1)
(没有检查有效性,但这些内容是这样的:
$numericText = array('zero','one','two','three','four','five','six','seven','eight','nine');
$price = '1,254.40';
$chars = str_split($price);
foreach($chars as $char) {
if(is_numeric($char)) {
$output .= $numericText[(int)$char] . " *";
} elseif($char == '.') {
$output .= 'usd *';
}
}
$output .= ' cents';