简体   繁体   中英

Converting this Excel formula to Javascript

My Javascript knowledge is very limited and I was wondering if someone could give me a hand converting this Excel formula into Javascript.

Please see the formula below

=ROUNDDOWN(
    IF(
        I7 = 10000,
        88.4% * (I7/107*7),
        IF(
            I7 = 20000,
            89.4 * (I7/107*7),
            IF(
                I7 = 40000,
                90.40 * (I7/107*7),
            )
        )
    ),
    0
)

I7 is the input

Basically, anything between 10000 and 19999 uses 88.4% * (I7/107*7)

20000 and 39999 uses 89.4 * (I7/107*7)

40000 and above use 90.40 * (I7/107*7)

Round down to the nearest whole number and output

Thanks in advance!

This is what you asked, not what the formula did

 const calculate = num => { if (num < 10000) return "N/A"; num = num / 107 * 7; let pct = 1/100; if (num < 20000) pct *= 88.4; else if (num < 30000) pct *= 89.4; else if (num >= 40000) pct *= 90.40; return Math.round(num * pct) } console.log( calculate(10), calculate(10000), calculate(15000), calculate(25000), calculate(50000) )

I write here a basic code so you can follow.

function calCell(input) {
  var value;
  switch(true)
  {
    case input >= 10000 && input < 20000 :
      value = Math.floor(88.4 / 100 * (input / 107 * 7));
    break;
    case input >= 20000 && input < 40000 :
          value = Math.floor(89.4 / 100 * (input / 107 * 7));
    break;
    case input >= 40000 :
      value = Math.floor(90.4 / 100 * (input / 107 * 7));
    break;
    default:
    //I don't know what to do if your value < 10000, but you can put it here
    break;
  }
  return value;
}

console.log(calCell(12345));

Most short and simplified!

function RoundDown(num){
if (num >= 10000 && num <20000) {num=num*88.4/100*107*7}
if (num >= 20000 && num <40000) {num=num*89.4/100*107*7}
if (num >= 40000) {num=num*90.4/100*107*7}
return Math.floor(num)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM