Click to See Complete Forum and Search --> : CSS number formatting


ajn2004
09-03-2004, 07:35 AM
Hi

Don't know if this is possible so I thought I'd ask you guys.

I need to format data so it is rendered on the page in 2 decimal places. Is it possible to do this with CSS?

The reason I need to do this is because the data is being exported to Excel and when this happens I lose the 2 decimal places:

E.g. £6.50 is displayed as £6.5

I have noticed that when the data is exported to Excel it keeps the CSS style formatting, so then thought if I could get CSS to enforce 2 decimal places on the .aspx page it would then enforce this in Excel?

Feel free to suggest other ways of doing this...

Thanks

AJN

Charles
09-03-2004, 08:05 AM
I've been wanting to do something like this for a while, but with dates…

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script type="text/javascript">
<!--
String.prototype.reverse = function () {return this.split('').reverse().join('')};

function Dollars (d) {this.ammount = typeof d == 'number' ? d : Number(d.toString().replace(/[$,]/g, ''))};

Dollars.prototype.valueOf = function () {return this.ammount};

Dollars.prototype.toString = function () {
if (isNaN (this.ammount)) return NaN.toString();
var l = Math.floor(Math.abs(this.ammount)).toString();
var r = Math.round((Math.abs(this.ammount) % 1) * 100).toString();
return [(this.ammount < 0 ? '-' : ''), '$', (l.length > 4 ? l.reverse().match(/\d{1,3}/g).join(',').reverse() : l),'.', (r < 10 ? '0' + r : r)].join('');
}

if (document.getElementsByTagName) onload = function () {
var e, i;
for (i = 0; e = document.getElementsByTagName('SPAN')[i]; i++) {if (/currency/i.test(e.className)) e.firstChild.data = new Dollars (e.firstChild.data)}
}
// -->
</script>

</head>
<body>
<ul>
<li><span class="currency">$1.25</span></li>
<li><span class="currency">$125</span></li>
<li><span class="currency">$12500</span></li>
<li><span class="currency">$1250000</span></li>
</body>
</html>

We had a little break with your currency a few years back but you should have no trouble adapting that to suit your needs.