在JavaScript中使用截断和舍入将浮点数转换为整数。
您可以尝试运行以下代码将float转换为整数:
<!DOCTYPE html> <html> <body> <p>Values:</p> <script> var myFloat = 1.5; var myTrunc = Math.trunc( myFloat ); var myCeil = Math.ceil( myFloat ); var myFloor = Math.floor( myFloat ); var myRound = Math.round( myFloat ); document.write(myTrunc+ " "); document.write(myCeil+ " "); document.write(myFloor+ " "); document.write(myRound+ " "); </script> </body> </html>