Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a numeric value into a percentage (or) append percentage symbol to a number?

Tags:

css

less

I'm trying to use LESS css to do the following:

width: ((480/1366)*100)+'%';

The problem though is that the output becomes:

width: 35.13909224011713 '%';

How do I make it workable? ie.:

width: 35.13909224011713%;
like image 785
Joel Avatar asked Aug 08 '11 06:08

Joel


People also ask

How do you convert a number into a percentage?

To convert a decimal to a percentage, multiply by 100 (just move the decimal point 2 places to the right). For example, 0.065 = 6.5% and 3.75 = 375%. To find a percentage of a number, say 30% of 40, just multiply. For example, (30/100)(40) = 0.3 x 40 = 12.

How do I add a percentage to a number in Excel?

You can add percentages like any other number. Choose a cell to display the sum of your two percentages. In this example, we're going to click and highlight cell C3. In the formula bar, type “=sum” (without quotes) and then click the first result, the sum formula, which adds all numbers in a range of cells.

How do you add a percent sign to a number in sheets?

Add Percentage Style to a Number in Google Sheets Select the cells with decimal numbers (C2:C8), and in the Menu, click the Percentage symbol. 2. To add decimals, (1) select the range with percentages (C2:C8), and in the Menu, (2) click the Increase decimal places icon.


2 Answers

Even though this question is quite old, I want to add a few more examples about adding. Less will set your units to whatever is being operated on.

10px + 20px

will output 30px

(20/200) * 100%

will output 10%

So with units you dont need to concatenate the unit measurement.

I have found that adding 0 helps when you dont know what the unit value might be.

.mixin(@x, @y){
    @result: (@x / @y) * 100;
}

.my_class {
    .mixin(20, 100);
    width: @result + 0%; // you can use any unit here
}

The above class will have a width of 20%. If we added with px, it would be 20px.

like image 142
Richard Testani Avatar answered Sep 28 '22 03:09

Richard Testani


It is possible to use string interpolation:

@myvar: ((480/1366)*100);
width: ~"@{myvar}%";

That will output

width: 35.13909224011713%;

Additionally, if you want it to be rounded, you can use round().

like image 21
ldiqual Avatar answered Sep 28 '22 01:09

ldiqual