user1082754
user1082754

Reputation:

Test if value is an integer in Sass

How can I test whether a value is an integer in Sass?

I saw some documentation stating there was an int? function but I don't think I'm using it correctly. That, or I have misunderstood the documentation.

I am currently doing this, but am getting an error:

int?(16)

Upvotes: 11

Views: 5620

Answers (3)

pentzzsolt
pentzzsolt

Reputation: 1131

Just to let you know, I put Chris' approach in a Sass function. You can find it on Github and on npm.

It does the same thing essentially, but wrapped in a reusable function.

You could use it like this:

.selector {
    @if is-int(16) {
        color: red;
    }
}

Which in this case outputs:

.selector {
    color: red;
}

Upvotes: 1

user1082754
user1082754

Reputation:

Chris Eppstein helped me work it out. Pretty easy in hindsight:

round($n) == $n

Upvotes: 17

just eric
just eric

Reputation: 927

  • (Bool) unitless(number)

Inspects the unit of the number, returning a boolean indicating if it is unitless.

Examples:

  unitless(100) => true

  unitless(100px) => false

Parameters: (Literal) number — The number to inspect

Returns: (Bool) — Whether or not the number is unitless

Raises: (ArgumentError) — if number isn’t a number

Upvotes: -2

Related Questions