Javascript删除最后一个字符并向上舍入

时间:2017-02-28 07:40:31

标签: javascript

假设我们有一个这样的字符串:2002。我想删除最后一个字符并舍入结果的最后一个字符。这意味着正确的输出是:201

注意:如果最后一个字符是0,我们不需要向上舍入;

更多示例:

100 --- to: --- 10

200 --- to: --- 20

101 --- to: --- 11

222 --- to: --- 23

30006 --- to: --- 3001

1299 --- to: --- 130

4089 --- to: --- 409

5099 --- to: --- 510

实际上它是货币转换的解决方案 Rial to Toman

4 个答案:

答案 0 :(得分:5)

"Content-Type: text/xml"除以10并使用number进行向上舍入。

Math.ceil

答案 1 :(得分:1)

var a=parseInt(prompt("enter a number"));
var result=Math.ceil(a/10);
alert(result);

答案 2 :(得分:1)



var rounder = function(num){
   return Math.ceil(num/10);
}

console.log(rounder(100));
console.log(rounder(101));




答案 3 :(得分:0)

function doYourJob(number){
  var withoutLastChar = number.slice(0,-1); //remove last char
  var toInteger = +withoutLastChar; //Convert to number
  return toInteger + 1; //Add 1
}

console.log(doYourJob("2002"));
console.log(doYourJob("222"));
console.log(doYourJob("1299"));