user1527354
user1527354

Reputation: 195

Formatting number to decimal

I understand how to format a number from for example 2.10 to 2.1 nut how would i format a number so that 381 -> 38.1% or 38.1

Either result is fine as I can just add a percent sign after the first result. I was thinking of maybe splitting the number then adding a decimal after the second number but this wouldn't work with 100%?

Upvotes: 0

Views: 69

Answers (3)

displayName
displayName

Reputation: 14359

Doing $number = $number / 10; should turn the trick.

Upvotes: 0

Rene Noel Cruz
Rene Noel Cruz

Reputation: 21

If 381 is a percentage of 1000, then 381 divided by 1000 = 0.381 Multiply 0.381 by 100 to get 38.1

The SQL CODE would be: DECLARE @Number1 Decimal(18,1) = 381, @Number2 Decimal(18,1) = 0

SET @Number2 = CAST(ROUND((@Number1 / 1000) *(100),1,1) AS DECIMAL(18,1))

SELECT @Number2

Hope this helps.

Upvotes: 2

Rizier123
Rizier123

Reputation: 59681

This should work for you:

$number = 381;
echo sprintf("%02.2f%%", $number/10);

Output:

38.10%

If you don't want it with 2 decimal just change .2 as you want it

Upvotes: 0

Related Questions