user15754268
user15754268

Reputation: 29

Conversion for GB to MB in js

I am trying to add a conversation logic for converting 1 GB to value 1000; 2.5 GB to value 2500; 50 MB to value 50; 1 MB to value 1 and so on using javascript.

I tried adding logic but it is comprising multiple if else.

var bw;
var value;
if (bw.toUpperCase() == '1 GB' || bw.toUpperCase() == '1 GBPS')
{
  value = 1000;
}
else if (bw.toUpperCase() == '2.5 GB' || bw.toUpperCase() == '2.5 GBPS')
{
  value = 2500;
}

and so on for MB. The only values that will come are GB or MB. No kb or bytes.

Upvotes: 0

Views: 741

Answers (1)

Ram
Ram

Reputation: 144689

One option is:

let units = { GB: 1000, GBPS: 1000, ... };
let [a, b] = yourString.split(' ');
let value = +a * units[b];

Upvotes: 1

Related Questions