Tuesday 3 December 2013

Decimal place

Hi,
 
Whenever we implement payment related things and we are using decimal values,
it happens that we need to allow 2 digit or three digit after decimal place.
Here is the code for the same. Below you can see two set of code.
 
One is for general use and other we can use at the place where we are using 
culture base implementation.
 
Simple:
 
This is the simple code to allow only two digit after decimal.
This will auto correct the value to two decimal place.
 
 function validNum(ctrl) {
        $(ctrl).val(parseFloat($(ctrl).val(), 10).toFixed(2));
    }
 
Culture based:

 Below code will show to use this when playing with multiple cultures.
 
var DecimalSeperator=',';  //This is for sweden culture 
   function roundDecimalValue(ctrl, roundedValue) {
        var value = $(ctrl).val().trim();
        if (value.length>0) {
            value = value.replace(DecimalSeperator, ".");
            value = parseFloat(value, 10).toFixed(roundedValue);
            // alert(val + "|" + $(ctrl).val());
            value = value.replace(".", DecimalSeperator);
            $(ctrl).val(value);
        }
    } 
 
 
Hope this can help you.
 

No comments:

Post a Comment