question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Formatting numbers during interpolation

See original GitHub issue

The following snippet:

$n: 0;
@while $n <= 10 {
    .cls-#{n} { /* ... */ }
    $n: $n + 1;
}

Produces a list of classes like:

.cls-0 {}
.cls-1 {}
.cls-2 {}
...
.cls-10 {}

Is there any way to zero-pad the numeric interpolation such that the list of classes have a consistent number of digits like this?

.cls-00 {}
.cls-01 {}
.cls-02 {}
...
.cls-10 {}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
ArmorDarkscommented, Apr 10, 2017

@compwright This is slightly crazy, but can be even better solution for you.

If you’re using libsass via node-sass, you can pass sprintf function to Sass via custom function.

sprintf already has great built-in padding functionality, and even more.

Do not forget to cast properly JS type to Sass types, though.

0reactions
chriseppsteincommented, Apr 10, 2017

Writing a function to do this is the preferred option. Numeric formatting just isn’t that important in CSS for the most part.

Read more comments on GitHub >

github_iconTop Results From Across the Web

string interpolation - format string output - Microsoft Learn
Any sequence of { or } shorter than the number of $ will be embedded in the output string. As shown in the...
Read more >
C# 6 how to format double using interpolated string?
For insensitive interpolation, you can use Invariant from System.FormattableString: Invariant($"at {num}") . See stackoverflow.com/questions/33203261/… – ANeves.
Read more >
#{...}: Numerical interpolation - Apache FreeMarker Manual
The numerical interpolation is used to output a number value. If the expression doesn't evaluate to a number, the evaluation ends with an...
Read more >
Aligning strings within string.Format and interpolated strings
You specify the alignment component after the number to format and a comma , . For example, the following format string "{value,5}" when...
Read more >
3 Must-Know Methods for Python String Interpolation
format (value = price, year_built = year))The house is built in 2000 and worth 100000. In case you are printing decimal point numbers...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found