0

My integer value is like below and

    var amount= 5501; 
    var amount= 5500;
    var amount= 5560;
    var amount= 5563;
    var amount= 5510;

i want split this integer like below .Have to add decimal point on middle. if last digit 0 not consider.

amount and should come "55.0.1" amount and should come "55.0" amount and should come "55.6" amount and should come "55.6.3" amount and should come "55.1"

i tried this ,

var variable1 = 5500;

function versionss(variable1){
var digits = (""+variable1).split("");
if(Number(digits[3] > 0)){
    return  Number(variable1/100) ; //
} else {
    return digits[0]+digits[1]+'.'+digits[2];
}

}
GOOG
  • 73
  • 1
  • 9

1 Answers1

1

Using Number.isInteger()

function versionss(amount) {
  var divisor = Number(amount) > 999 ? 100 : 10;
  var value = amount / divisor;
  return Number.isInteger(value) ? value.toFixed(1) : value;
}

console.log(versionss(5501)); 
console.log(versionss(5500)); 
console.log(versionss(5560)); 
console.log(versionss(5563)); 
console.log(versionss(5510)); 
User863
  • 19,346
  • 2
  • 17
  • 41
  • Hi one more change i need, If its 5501 means i want to split 55.0.1 and 5500 means 55.0. last digit more than 0 then need one more decimal – GOOG Mar 26 '20 at 13:51
  • What you are asking is something not parallel to this question. You can ask a new question with what have you tried – User863 Mar 26 '20 at 13:54
  • If fourth digit more than ZERO means i want to split with 2 decimal points.. 5501 => 55.0.1 and 5500 => 55.0 – GOOG Mar 26 '20 at 14:02