$(document).ready(function(){
cd.chdecimal(".chdec");
});
cd ={
chdecimal: function(cl){
$.each($(cl), function(){
var clearString = ($(this).html()).toString();
var n = parseFloat($(this).html());
var pNumber = n.toFixed(2);
var str = pNumber.toString();
var strl1 = str.substr(str.length-1, str.length-1);
var strl2 = str.substr(str.length-2, 1);
var result;
if(strl1 === "0" && strl2 === "0"){
result = parseInt(pNumber);
}
else if(strl2 === "0" && strl1 !== "0"){
result = clearString.replace(".", ",");
}
else if(strl1 === "0" && strl2 !== "0"){
var res1 = n.toFixed(1);
var res2 = res1.toString();
result = res2.replace(".", ",");
}
else{result = clearString.replace(".", ",");}
$("#output").append("int:"+pNumber+" | string:"+str+" | "+strl2+" | "+strl1+" | result:"+result+"<br>");
});
}
}
// 2. Example of HTML
/*
<div class="container">
<div class="row">
<div class="col-sm-6">
<h1>Input</h1>
<span class="chdec">12.20</span><br/>
<span class="chdec">12.0</span><br/>
<span class="chdec">12.12</span><br/>
<span class="chdec">12.02</span><br/>
<span class="chdec">12.01</span><br/>
<span class="chdec">12.1</span><br/>
<span class="chdec">12.9</span><br/>
<span class="chdec">0.5</span><br/>
<span class="chdec">12.9</span><br/>
</div>
<div class="col-sm-6">
<h1>Output</h1>
<pre id="output"></pre>
</div>
</div>
</div>
*/
Example of function which converts decimal numbers
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.