Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding numbers in Sass and adjusting the amount of decimals

Is their a way in sass to change the digit that rounding occurs on, I would like to stop rounding of a number like this 2.0242914979757085% to this 2.024%. I would like to adjust the amount of decimals in the output css

like image 235
Lawrence Avatar asked Apr 29 '12 04:04

Lawrence


People also ask

How do you round to different decimal places?

To round a number look at the next digit in the right place, if the digit is less than 5, round down and if the digit is 5 or more than 5, round up. Rounding decimals refer to the rounding of decimal numbers to a certain degree of accuracy. We can round decimals to the nearest wholes, tenths or hundredths.

How do you limit decimal places?

Right click the selected cells, and select the Format Cells from the right-clicking menu. 3. In the coming Format Cells dialog box, go to the Number tab, click to highlight the Number in the Category box, and then type a number in the Decimal Places box.

How do you round to 2 decimal places in numbers?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.

Which numeric function is used to round up the number to whole in sass?

Bounding Functions permalinkBounding Functions Rounds $number up to the next highest whole number.


3 Answers

A quick option without any extra functions would be to multiply the number by 1000, then round it, then divide by 1000.

round($percent * 1000) / 1000;
like image 59
Jargs Avatar answered Oct 17 '22 04:10

Jargs


From the SASS change logs:

The numeric precision of numbers in Sass can now be set using the --precision option to the command line. Additionally, the default number of digits of precision in Sass output can now be changed by setting Sass::Script::Number.precision to an integer (defaults to 3). Since this value can now be changed, the PRECISION constant in Sass::Script::Number has been deprecated. In the unlikely event that you were using it in your code, you should now use Sass::Script::Number.precision_factor instead.

This was added in SASS 3.1.8.

like image 37
Bill Avatar answered Oct 17 '22 05:10

Bill


You could use the following function, which is a slight improvement of the function created by Takeru Suzuki :

@function decimal-round ($number, $digits: 0, $mode: round) {
    $n: 1;
    // $number must be a number
    @if type-of($number) != number {
        @warn '#{ $number } is not a number.';
        @return $number;
    }
    // $digits must be a unitless number
    @if type-of($digits) != number {
        @warn '#{ $digits } is not a number.';
        @return $number;
    } @else if not unitless($digits) {
        @warn '#{ $digits } has a unit.';
        @return $number;
    }
    @if $digits > 0 {
        @for $i from 1 through $digits {
            $n: $n * 10;
        }
    }
    @if $mode == round {
        @return round($number * $n) / $n;
    } @else if $mode == ceil {
        @return ceil($number * $n) / $n;
    } @else if $mode == floor {
        @return floor($number * $n) / $n;
    } @else {
        @warn '#{ $mode } is undefined keyword.';
        @return $number;
    }
}

Output :

decimal-round(0.333)    => 0
decimal-round(0.333, 1) => 0.3
decimal-round(0.333, 2) => 0.33
decimal-round(0.666)    => 1
decimal-round(0.666, 1) => 0.7
decimal-round(0.666, 2) => 0.67
like image 41
John Slegers Avatar answered Oct 17 '22 05:10

John Slegers