PHP元转分的错误示范代码分析

后端开发   发布日期:2023年06月06日   浏览次数:221

本篇内容主要讲解“PHP元转分的错误示范代码分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PHP元转分的错误示范代码分析”吧!

错误的示范。

$price=20.08;
echo yuan_to_fen($price);

function yuan_to_fen($price)
{
  return intval(100 * $price);
}

function yuan_to_fen2($price)
{
  return floor(100 * $price);
}
function yuan_to_fen3($price)
{
  return (int)(100 * $price);
}

正确的示范

$price=20.08;
echo yuan_to_fen($price);

function yuan_to_fen($price)
{
  return round(100 * $price);
}

汇总

<?php
$a = 20.08;
$b = yuan_to_fen1($a);
$c = yuan_to_fen2($a);
$d = yuan_to_fen3($a);
$e = yuan_to_fen4($a);

$x = yuan_to_fen31($a);
$y = yuan_to_fen32($a);
$z = yuan_to_fen33($a);

echo('价格=' . $b . "\n");//2007
echo('价格2=' . $c . "\n");//2007
echo('价格3=' . $d . "\n");//2007
echo('价格4=' . $e . "\n");//2008

echo('方法31=' . $x . "\n");//2008
echo('方法32=' . $y . "\n");//2008
echo('方法33=' . $z . "\n");//2008

function yuan_to_fen1($price)
{
  return intval(100 * $price);
}

function yuan_to_fen2($price)
{
  return floor(100 * $price);
}

function yuan_to_fen3($price)
{
  return (int)(100 * $price);
}

function yuan_to_fen4($price)
{
  return 100 * $price;
}

function yuan_to_fen31($price)
{
  return intval(strval(100 * $price));
}

function yuan_to_fen32($price)
{
  //返回浮点数。
  return round(100 * $price);
}

function yuan_to_fen33($price)
{
  //返回整型,比较推荐这种。
  return intval(round(100 * $price));
}

以上就是PHP元转分的错误示范代码分析的详细内容,更多关于PHP元转分的错误示范代码分析的资料请关注九品源码其它相关文章!