第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何舍入浮點數(shù)?

如何舍入浮點數(shù)?

SMILET 2022-07-21 09:51:02
JavaScript 中是否有一些有用的函數(shù)可以幫助將任何浮點數(shù)舍入到最近的鄰居,無論是整數(shù)還是帶有 .5 的浮點數(shù)?Input -> Output:  - > 2.1 -> 2.0   > 2.4 -> 2.5   > 1.9 -> 2....javascript
查看完整描述

3 回答

?
嗶嗶one

TA貢獻1854條經(jīng)驗 獲得超8個贊

您可以使用以下功能:


var intvalue = Math.floor( floatvalue );

var intvalue = Math.ceil( floatvalue ); 

var intvalue = Math.round( floatvalue );


// `Math.trunc` was added in ECMAScript 6

var intvalue = Math.trunc( floatvalue );


查看完整回答
反對 回復(fù) 2022-07-21
?
冉冉說

TA貢獻1877條經(jīng)驗 獲得超1個贊

不是最短但工作

function roundNumberWith05 (num){

  const diff = num - Math.floor(num);

  if (diff < 0.25 || diff > 0.75) {

    return Math.round(num * 2) / 2;

  } else {

    return num - diff + 0.5;

  }

}

console.log('2.1 --', roundNumberWith05(2.1));

console.log('2.4 --', roundNumberWith05(2.4));

console.log('1.9 --', roundNumberWith05(1.9)); 

console.log('1.75 --', roundNumberWith05(1.75)); 

console.log('1.74 --', roundNumberWith05(1.74)); 

console.log('1.76 --', roundNumberWith05(1.76)); 

console.log('2.688 --', roundNumberWith05(2.688)); 

console.log('2.2588 --', roundNumberWith05(2.2488)); 


查看完整回答
反對 回復(fù) 2022-07-21
?
白豬掌柜的

TA貢獻1893條經(jīng)驗 獲得超10個贊

嘗試這個,


function my_round(x){

  return Math.floor(x) + Math.round((x - Math.floor(x)) * 2) / 2 

}

或者更好的是,使用@ritaj 建議的方法


function myRound(x){

  return Math.round(x * 2)/2 

}


查看完整回答
反對 回復(fù) 2022-07-21
  • 3 回答
  • 0 關(guān)注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號