Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
http://jsfiddle.net/s6rCN/5/

For example, instead $123,456,789

it should be

1234567.89

What I have tried:

How can i fix it ? Thank youu.
Posted
Updated 30-Jan-18 23:10pm

1 solution

var format = function(num){
	var str = num.toString().replace("$", ""), parts = false, output = [], i = 1, formatted = null;
	if(str.indexOf(".") > 0) {
		parts = str.split(".");
		str = parts[0];
	}
	str = str.split("").reverse();
	for(var j = 0, len = str.length; j < len; j++) {
		if(str[j] != ",") {
			output.push(str[j]);
			if(i%3 == 0 && j < (len - 1)) {
				output.push("");
			}
			i++;
		}
	}
	formatted = output.reverse().join("");
	return( formatted + ((parts) ? "." + parts[1].substr(0, 2) : ""));
};
$(function(){
    $("#currency").keyup(function(e){
        $(this).val(format($(this).val()));
    });
});
 
Share this answer
 
Comments
Member 13582084 31-Jan-18 5:12am    
sorry not working
[no name] 31-Jan-18 5:14am    
i have already running the same in your js fiddle mentioned above and its running as per your expectation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900