Home  >  Article  >  Backend Development  >  How to convert int (integer) type to decimal in php

How to convert int (integer) type to decimal in php

青灯夜游
青灯夜游Original
2022-03-03 19:22:573003browse

Conversion method: 1. Use the number_format() function, the syntax "number_format (int type value, number of decimal places)"; 2. Use the sprintf() function, the syntax "sprintf("%.xf", value of type int)", the parameter x is the number of decimal places.

How to convert int (integer) type to decimal in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php will int ( Integer) type to decimal

Method 1: Use the number_format() function

number_format() can format numbers by thousands grouping .

<?php
echo number_format(12,1)."<br>";
echo number_format(12,2)."<br>";
echo number_format(12,3)."<br>";
echo number_format(12,4)."<br>";
?>

How to convert int (integer) type to decimal in php

Method 2: Use sprintf() function

sprintf() function writes the formatted string into a variable middle.

<?php
echo sprintf("%.1f",12)."<br>";
echo sprintf("%.2f",13)."<br>";
echo sprintf("%.3f",14)."<br>";
echo sprintf("%.4f",15)."<br>";
?>

How to convert int (integer) type to decimal in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert int (integer) type to decimal in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn