Tom Hanson
Tom Hanson

Reputation: 945

Kendo Number Formattig For unwanted zeros

I am looking to filter my kendo.toString to knock off the unwanted zeros leaving a two decimal place minimum and 4 decimal place maximum. for example...

my actual markup is

<li><label>Flagfall:</label>$#= (Flagfall == null) ? '0.00' : kendo.toString(Flagfall, "n4") #</li>

Upvotes: 0

Views: 1073

Answers (3)

Sunny Patel
Sunny Patel

Reputation: 8077

Your format string can be changed to be kendo.toString(Flagfall, "#.00##"). The 0 placeholders denote required slots to be filled, whereas the # placeholders denote the optional positions. Kendo seems to handle this nicely:

var numbers = [42, 42.0, 0, 0.01, 0.0003, 5.2, 4.12351, 4.12355],
  template = "kendo.toString(num, '#.00##')"
  tpl = kendo.template("Number: #= num #\t- #= " + template.replace(/#/g, "\\\\#") + "#  \t Formatted\n"),
  $text = $("#text");

numbers.forEach(function(num) {
  $text.val($text.val() + tpl({
    num: num
  }));
});
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.2.607/js/kendo.all.min.js"></script>

<textarea id="text" style="height: 10em; width: 30em;"></textarea>

If you were using the Kendo Numeric Textbox, you can use the format string combined with the decimals property.

$("#numerictextbox").kendoNumericTextBox({
  format: "#.00##",
  decimals: 4
});
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.silver.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.2.607/js/kendo.all.min.js"></script>

<input id="numerictextbox" />

Upvotes: 1

Azamantes
Azamantes

Reputation: 1461

I've tested my proposal against the examples you've given. I've tested it against numbers. If you have a string containing a number then just use it like:

var result = convert(+string);

Anyways, here's the solution:

var x = 25.000;
function convert(value) {
    let [a, b] = ('' + value).split(/\./);
    b = b? b.replace(/([0-9]{2}[1-9]*9+)/, '$1') : '00';
    return a + '.' + b;
}
console.log(convert(25.7670));
console.log(convert(250));
console.log(convert(25.7080));
console.log(convert(25.000)); // same as 25, honestly

The function returns a string. If you want it to be number, just put + before it, like:

var result = +convert(your_number_here);

Or if you prefer/need old Javascript version:

function convert(value) {
    var array = ('' + value).split(/\./);
    var a = array[0];
    var b = array[1];
    b = b? b.replace(/([0-9]{2}[1-9]*9+)/, '$1') : '00';
    return a + '.' + b;
}

Upvotes: 1

Hector Barbossa
Hector Barbossa

Reputation: 5528

You can write a custom javascript function like

var isInt = function(n) { return parseInt(n) === n };

function format(num){
  if(isInt(num)){
    return Number(num).toFixed(2);
  }else{
    return Number(num).toFixed(3);
  }
}

console.log(format(25.7670));

to achieve this.

Upvotes: 0

Related Questions