私はphpjsという JavaScript ライブラリを使用しています 。目標は、PHP が関数を処理する方法を模倣する関数を作成することです。
特に 1 つの関数を次に示します: http://phpjs.org/functions/is_float:442
私はいくつかのテストケースを書きましたが、すべてが期待どおりです。ただし、これを試すとすべてが壊れます:
document.write(is_float(16.0x00000000));
true または false が画面に表示されることはなく、空の空白のみが表示されます。どうしてこれなの?
私がそれをしている間、その関数でreturn !!(mixed_var % 1);
は、ダブルとは何ですか!!と言っています。為に?私は前にこれに遭遇したことはありません。ソース コードで 1 つを除外すると、次のテスト ケースでまったく同じ結果が得られます。私が忘れている可能性のあるケースはありますか?
document.write(is_float(186.31));document.write('<br>');
document.write(is_float("186.31"));document.write('<br>');
document.write(is_float("number"));document.write('<br>');
document.write(is_float("16.0x00000000"));document.write('<br>');
document.write(is_float("16"));document.write('<br>');
document.write(is_float(16));document.write('<br>');
document.write(is_float(0));document.write('<br>');
document.write(is_float("0"));document.write('<br>');
document.write(is_float(0.0));document.write('<br>');
document.write(is_float("0.0"));document.write('<br>');
document.write(is_float("true"));document.write('<br>');
document.write(is_float(true));document.write('<br>');
document.write(is_float("false"));document.write('<br>');
document.write(is_float(false));document.write('<br>');
編集: 0.0 の問題について、これは修正できません。ドキュメントを確認してください。
//1.0 is simplified to 1 before it can be accessed by the function, this makes //it different from the PHP implementation. We can't fix this unfortunately.
これは、JavaScript が独自に行っていることのようです。プログラマーはこれを制御できません。