SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    Are You There? KDesigns's Avatar
    Join Date
    Oct 2003
    Location
    Your Monitor
    Posts
    1,146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rounding Problem

    Hello, I'm having a problem with rounding in a Javascript file. If an order comes up at 17.99 it rounds to 18.00. Please help, this is urgent.

    Code:
    function moneyFormat(amount) {
    var	fixValue = parseFloat(Math.pow(10,2));//2 = number of dec places.
    var	retValue = parseInt(Math.round(amount * fixValue)) / fixValue;
    var result=((parseFloat(amount)*100))/100;
    	amount=result;
    
    	pounds=parseInt(amount);
    	pence=amount-pounds;
    	if ((pence > 0.90) && (pence < 0.91)) {
    		amount=pounds+0.90;
    	}
    	if (pounds==amount) {
    		return amount+".00";
    	} else if (parseInt(amount*10)==amount*10) {
    		return amount+"0";
    	} else {
    		return (parseFloat((Math.ceil(amount*100))/100));
    	}
    }
    ChooseDaily.com - Follow on Twitter
    Top Resources for Web Designers and Developers Every Day!

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried toFixed?

    Also this page has more information on number formatting than you will ever need.

    http://www.merlyn.demon.co.uk/js-round.htm

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    toFixed() doesn't return the same values for floating point numbers in different browsers-
    IE truncates, but Mozilla rounds the last digit,
    and I forget what Opera and Safari do.

    If you want to get the same value in all browsers you
    can explicitly tell the script to round or trim to the last significant digit:

    Code:
    	Number.prototype.fixTrim= function(n){
    		var N= this;
    		N= Math.floor(N*Math.pow(10,n));
    		N/=Math.pow(10,n);
    		return(N.toFixed(n));
    	}
    	Number.prototype.fixRound= function(n){
    		var N= this;
    		N= Math.round(N*Math.pow(10,n));
    		N/=Math.pow(10,n);
    		return(N.toFixed(n));
    	}

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    toFixed() doesn't return the same values for floating point numbers in different browsers-
    IE truncates, but Mozilla rounds the last digit,
    Actually, this is a bug in Internet Explorer that numbers in range 0.5 - 0.95 are truncated. eg. (0.6).toFixed(0) gives 0. This can be corrected by setting integer part to not-null, e.g.
    Code:
    fixed = ((i + 1).toFixed(2) - 1).toFixed(2);

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    num.toFixed(0) should be written parseInt((num)).
    or to round it to an integer: Math.round(num)

    If I want to round 1.19999 to 2 significant digits
    I want 1.20, which is 'closer' than 1.19 to the actual floating point value.
    Last edited by mrhoo; Dec 10, 2006 at 16:20. Reason: correction

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    num.toFixed(0) should be written parseInt(num).
    ECMA standard says toFixed returns a number rounded to the given precision. So, (1.8).toFixed(0) should (and does) return 2, whereas parseInt(1.8) returns 1.

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, (1.8).toFixed(0) should (and does) return 2
    Yes, but (.8).toFixed(0) returns 0 in IE and 1 in others.

    if you want the integer part of a number use parseInt(num),
    if you want a rounded whole number use Math.round(num).
    Use toFixed(n) if you actually want significant digits.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •