Home Wrap fractional part of decimal in a span
Reply: 2

Wrap fractional part of decimal in a span

Mikhail Published in 2018-02-13 21:22:05Z

I am printing a variable (called something.price) to a Haml template and its value is a string representation of a decimal with 2-digit precision. I want to wrap the fractional part of the decimal (including the .) in a span.

Example: If we have "1234.41", we want to wrap the span around the .41 part. How can this be achieved?

Zoran Reply to 2018-02-13 22:04:19Z

Since you know the format of your original string (decimal, two digit precision, etc.), you can wrap the decimal point portion of that string in a <span> by doing something like the following:

price  = '1234.41'
result = price.sub('.', '<span>.').concat('</span>')
# => "1234<span>.41</span>"

Hope this helps!

Marcos R. Guevara
Marcos R. Guevara Reply to 2018-02-13 21:35:56Z

You can first convert to string with to_str method if it's a number, then, use the .split method, something like this:

number = "1234.41"

then do something like this


I ignore if you are doing this on backend or frontend, anyway you can find this split method on javascript. Once you have the array, you can iterate with the result.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306263 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO