AndrewFerrara
AndrewFerrara

Reputation: 2403

Percent from decimal? PHP

Trying to convert this "0.274509817" to a nice precentage like 27%

The string is a dynamic value from an API.

Upvotes: 20

Views: 38649

Answers (5)

Scott C Wilson
Scott C Wilson

Reputation: 20016

and after that, append

"%" 

to get the percent sign (e.g. if used with sprintf).

Upvotes: 0

MestreLion
MestreLion

Reputation: 13676

round((float)$value * 100) . '%'

Upvotes: 0

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74202

$number = 0.274509817;
echo round( $number * 100 ), '%';

Upvotes: 2

chx
chx

Reputation: 11760

What's the problem with intval($a * 100) . '%'?

Upvotes: 0

Alec Gorge
Alec Gorge

Reputation: 17390

$percent = round((float)$str * 100 ) . '%';

Where $str = "0.274509817"

Upvotes: 48

Related Questions