1

can somebody point me in the right direction please to remove decimal places on a different length of a number that sits within a span i.e. the prices of products on a website.

£6.9800

The above should say £6.98

We have a website where some prices are needed to be in 4 decimal places e.g £0.0260 and we have our site setup to display this. But we have other prices where this is not needed e.g. above price and £0.99, £1.99, £10.99, £100.99, £1000.99

Is there a way I can restrict prices to 2 decimal places without any rounding?

I can use JS/Jquery (because my CMS lets me).

I've tried trim and such like but I cant seem to cater for different length numbers (I'm probably just being dumb)

I thought I could use something like this but I'm not 100% sure: http://www.mredkj.com/javascript/numberFormat.html

user3456188
  • 69
  • 1
  • 6

3 Answers3

2

Use "toFixed" method. In this way:

var f = 1.2345;
f.toFixed( 2 ); 

EDIT: You can see the Gumbo's answer in this question: Display two decimal places, no rounding

Regards, Kevin

Community
  • 1
  • 1
kevinoo
  • 117
  • 10
0

Assuming the amount is not a string:

var f = 1.556;
Math.floor(1.556*100)/100;
Bernhard Hofmann
  • 10,321
  • 12
  • 59
  • 78
0

Well this is an example using jQuery

Html assuming that the prices are in the tags.

<p>0.0260</p>
<p>0.1260</p>
<p>6.9800</p>
<p>100.99</p>
<p>1.99</p>
<p>10.99</p>
<p>1000.99</p>

Script

  $.each($("p"),function(){
    var number = parseFloat($(this).text())
    var integerPart = number.toString().split(".")[0] == 0 ? 0: number.toString().split(".")[0].length;
    var decimalPart = number.toString().split(".")[1].length;

    if(decimalPart){
       $(this).text(number.toPrecision(integerPart + decimalPart.length))
    }else{
       $(this).text(number.toPrecision(integerPart + 2))
    }

}) 

Fiddle

Eduardo Quintana
  • 2,368
  • 1
  • 15
  • 20
  • great thank you - how would this be edited if the number was a html value within a span please with a currency symbol? i.e. £0.0260 – user3456188 Apr 14 '14 at 08:04
  • @user3456188 Take a look at this fiddle i just removed and added the currency symbol from the span's text http://jsfiddle.net/dH8S9/3/ – Eduardo Quintana Apr 14 '14 at 14:41
  • thanks for your help but £0.0260 is still coming out as such - that should be £0.026 and £6.9800 should be £6.98 - many thanks – user3456188 Apr 15 '14 at 08:28
  • @user3456188 Sorry a few mistakes http://jsfiddle.net/dH8S9/4/ – Eduardo Quintana Apr 15 '14 at 14:31
  • awesome thanks - one last tweak I'm struggling with is how to handle figures such as £1.00000 or £1.1000 - to get that to read £1.00 and £1.10? – user3456188 Apr 15 '14 at 15:22
  • im still struggling with this if somebody could help please. The above code works great where the price has numbers greater than 0 after the decimal point. But it fails if the price is e.g. £1.0000 or £10.0000 - I would like to those to become £1.00 or £10.00 - many thanks – user3456188 Jul 17 '14 at 14:03
  • Hi @user3456188 take a look at the updated answer I hope it helps you. – Eduardo Quintana Jul 21 '14 at 14:05