1

My question is about to float number in jquery. I looked other questions about it in this site but I couldn't find which I look.

In part of my code, I used $.post in jquery. I send request to database and fetch data as json format. In my database, some numbers' format is like 345,54565000000. So, I want to float numbers 5 digit after comma.

Part of my code is :

$.post("http://localhost/ajax.php",
      function(data) {
          var listdata;
          $.each(data, function(i,item){

              listdata += "<td>"+item.number+"</td>";

          });

            $(".result).empty().html(listdata);

    },"json"
);

Some of my trials are like:(which is not worked)

1.)

var number = (item.number).toFixed(5);
listdata += "<td>"+number+"</td>"; 

2.)

var number=item.number;
var new_number = number.toFixed(5);
listdata += "<td>"+new_number+"</td>"; 

Thank you for your replies.

2
  • number = number.toString().split("."); number[1].=substr(0, 5); number = (number[0] + "" + number[1]); Commented Dec 8, 2012 at 23:51
  • are you using a comma or a period? Rather, do you want the output as a comma or as a decimal? Commented Dec 8, 2012 at 23:56

4 Answers 4

3
var number = 345.54565000000​;

var parsedNumber = parseFloat(parseInt(number*100000,10)/100000);

FIDDLE;

If that seperator is in fact a comma, you'll have to replace it with .replace(',', '.')

Sign up to request clarification or add additional context in comments.

Comments

1

I use this function:

function roundNumber(number,decimals) {
    var newString;// The new rounded number
    decimals = Number(decimals);
    if (decimals < 1) {
        newString = (Math.round(number)).toString();
    } else {
        var numString = number.toString();
        if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
            numString += ".";// give it one at the end
        }
        var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
        var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
        var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
        if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
            if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
                while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
                    if (d1 != ".") {
                        cutoff -= 1;
                        d1 = Number(numString.substring(cutoff,cutoff+1));
                    } else {
                        cutoff -= 1;
                    }
                }
            }
            d1 += 1;
        } 
        newString = numString.substring(0,cutoff) + d1.toString();
    }
    if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
        newString += ".";
    }
    var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
    for(var i=0;i<decimals-decs;i++) newString += "0";
    return newString;
}

So in your code, change line 6 to:

listdata += "<td>"+ roundNumber(item.number, 5) +"</td>";

1 Comment

Thank you for your reply. It works; but without this huge function, other replies does work. Thank you for your interest.
1

I assume you have commas, and you want to stick with commas, so that will break any Float related functions, so... quick and dirty, but does the job:

Replace

listdata += "<td>"+item.number+"</td>";

With

listdata += "<td>"+parseFloat(item.number.replace(',', '.')).toFixed(5).replace('.', ',')+"</td>";

Comments

1

the fasted way I've found is

((number * Math.pow(10, numberOfDigits)) | 0) / Math.pow(10, numberOfDigits)

EDIT: forgot the Math.pow, that's important.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.