Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

simple regex in php, formatting decimal number

I need to convert numbers to have .00 after them, but only if the number is an integer, or it has just 1 number after the decimal point, like so:

1.4 = 1.40
45 = 45.00
34.77 = 34.77

What reg exp to use for this simple case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can also use printf or sprintf

printf("%01.2f", '34.77');
$formatted_num = sprintf("%01.2f", '34.77');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...