使用CSS将数字格式化为货币形式

53

有没有人知道是否可能仅使用CSS格式化元素内容为货币?如果可能的话,最好能够在CSS中指定值的呈现方式,但我并没有找到相关信息,所以不抱有太大期望 :)

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        .dollars:before { content:'$'; }
    </style>
</head>
<body>
    Pure CSS: <span class="dollars">25153.3</span>
    <br />
    Ideal format: <span>$25,153.30</span>
</body>
</html>

这个例子的输出结果如下:

纯CSS: $25153.3

理想格式: $25,153.30

我知道使用JavaScript很容易实现 - http://css-tricks.com/snippets/javascript/format-currency/.


不行,不能只用纯CSS实现。 - Scott
可能是https://dev59.com/Lmoy5IYBdhLWcg3wPbyd的重复问题。 - BoltClock
4个回答

36

货币格式可以通过CSS和一点Javascript实现,需要对数字进行解析并添加逗号。CSS类添加了额外的样式,如负数(红色)或货币符号(即美元符号 $)。操作步骤如下:

1) 将值转换为数字(根据语言环境添加逗号)

Number(value).toLocaleString('en');

2)添加一个类来确定这是正值还是负值(即红色)

.enMoney::before {
    content:"$";
}
.negMoney {
    color:red;
}

在这里查看更多详细信息,包括示例代码和 CSS:

http://www.ozkary.com/2014/04/format-currency-with-javascript-and-css.html


无法影响千位分隔符,但在数字前后就足够实现目标了... - DavidTaubmann
document.querySelectorAll(".currency").forEach(e => { n = Number(e.innerHTML).toFixed(2); if (!isNaN(n)) e.innerHTML = n.toLocaleString(); }); - user3270784

19

var number = 25153.3; console.log(number.toLocaleString()); /------

var number = 25153.3;
result="$ " + number.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})
console.log(result);

console.log();
//---------------------------------------------------

// request a currency format
console.log(number.toLocaleString('us-US', { style: 'currency', currency: 'USD' }));
// → $ 251,52.30 

console.log(number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
// → 25.152,30 €

// the Japanese yen doesn't use a minor unit
console.log(number.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }))
// → ¥251,53


4
这是 JavaScript 而不是 CSS。 - user3285954
2
但这是一个更好的解决方案。 - 13garth
23
不可以,因为它没有提供OP所要求的内容。 - btk

4

如果您在问CSS中的数字格式化(即从字符串中解析数字,然后使用千位分隔符、小数点分隔符、固定小数位等进行格式化),那么不,CSS无法做到这一点,这也不是CSS设计的初衷。

如果您想要进行任何格式化,最好使用XSLT。例如:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="span[@class='dollars']">
        <span>
            <xsl:text>$</xsl:text>
            <xsl:value-of select="format-number(current(), '###,###.00')"/>
        </span>
    </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

81
“这并不是CSS的设计目的”……我会保留对CSS设计目的的评判。将常见功能推入浏览器正是像CSS这样的技术所设计的。曾经有一段时间,“响应屏幕尺寸”也被认为“不是CSS的设计目的”。此外,请参考:http://wiki.csswg.org/ideas/content-formatting。 - umassthrower
8
啊哈,原来 XSLT 就是为此而设计的;-) - Simon_Weaver
41
“格式化数字”是一种展示的功能,而这正是CSS设计的目的。 - Aaron Cicali

-2

很遗憾,这并不适用于您自己的用例,而且显然,但是您可以对从-999到999的整数执行此操作。

.currency:before{ content:'$'; }
.currency:after{ content: '.00'; }
<span class="currency">789</span>

如果你是在服务器端工作,那么一种可能会让人感到烦恼的解决方案是将数字转换为反转字符串,并循环遍历每个字符。如果你真的想要,你可以将字符放入li中,然后css可以按照你想要的格式进行格式化。然而,这非常无意义,因为此时你可以直接编写字符串。

.currency li:before{ content: ' ,';}
.currency li:first-child:before{ content:'$' !important; }
.currency *{ display: inline-block; }
.currency, .currency > *{ display: inline-block; }
.currency:after{ content: '.00'; }
<ul class="currency"><li>123</li><li>456</li><li>789</li></ul> 
<ul class="currency"><li>456</li><li>789</li></ul> 
<ul class="currency"><li>789</li></ul> 

如果你想更深入地了解毫无意义的努力,你可以在前面加上一个

<style> .currency:after{ content: '.00'; } </style>

<ul>之上,允许您的脚本更改CSS中的十进制值,哈哈

不过,如果您妥协并使用JavaScript,那么CSS实际上可能会有些用处。您可以输出一个普通的int或double(任何精度),然后只需让JS将其分解为<li>


好主意。但不是正确的解决方案。因为如果您的整数增加到10万,您将没有逗号。 - 13garth
@GarthBaker 嗯,我很困惑为什么第二个代码片段可以使用6位和9位数字,但你的意思是什么呢?虽然我可以理解人们不喜欢这种解决方案,因为我也讨厌它并且不会使用它,但在格式化时逻辑地分组数字组相对于目标而言是语义化的。 - That Realty Programmer Guy
我不确定你在说什么。制作像这样的数字<ul class="currency"><li>123</li><li>456</li><li>789</li></ul> 是荒谬的,没有人会经历这样一个荒谬的过程。而且由于小数点是用CSS设置的,所以存在误差的可能性。这个解决方案是一个糟糕的hack。CSS不是解决这个问题的正确方法。 - 13garth
不是每个问题的正确解决方案,但在某些情况下确实是最理想的解决方案。至于错误的余地,您可能在逻辑上跳过了某些步骤。将<span class="formatIntAsCurrency">1234567890</span> 转换成这种形式是微不足道的,并且没有任何错误的机会。尽管您变得更加激进,而不是纠正有关100,000的错误,这真的很有趣。 - That Realty Programmer Guy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接