Reputation: 37
I have a script, where I can convert gigs to megs to kilobytes to bytes to bits.
After that it uses the .toExponential
function to turn it into scientific notation.
But I want it to change into an exponent, instead of it being +e#
I want it to be ^#
, any way I can change it to print that way instead, if not, anyway I can alter the string to change +e
to ^
?
Code:
console.log('calculator');
const gigabytes = 192;
console.log(`gigabytes equals ${gigabytes}`);
var megabytes = gigabytes * 1000;
console.log(`megabytes = ${megabytes}`);
var kilabytes = megabytes * 1000;
console.log (`kilabytes = ${kilabytes}`);
bytes = kilabytes * 1000;
console.log(`bytes = ${bytes}`);
bites = bytes * 8;
console.log(`bites are equal to ${bites}`);
console.log (bites.toExponential());
Upvotes: 2
Views: 709
Reputation: 19813
You can use .replace
:
const bytes = '1.536e+12'
console.log(convert(bytes))
const inputs = [
'1.536e+12',
'1.536e-12',
'123',
'-123',
'123.456',
'-123.456',
'1e+1',
'1e-1',
'0e+0',
'1e+0',
'-1e+0'
]
function convert(value) {
return value.replace(/e\+?/, ' x 10^')
}
inputs.forEach(i => console.log(convert(i)))
Upvotes: 2
Reputation: 54
Instead of using
console.log(bites.toExponential);
You may need to use
bites = bites.toExponential;
console.log(bites.replace(“e+”, “*10^”);
Hope this helped!
Upvotes: 1
Reputation: 36564
You can just replace e+
with ^
using replace
console.log('calculator');
const gigabytes = 192;
console.log(`gigabytes equals ${gigabytes}`);
var megabytes = gigabytes * 1000;
console.log(`megabytes = ${megabytes}`);
var kilabytes = megabytes * 1000;
console.log (`kilabytes = ${kilabytes}`);
bytes = kilabytes * 1000;
console.log(`bytes = ${bytes}`);
bites = bytes * 8;
console.log(`bites are equal to ${bites}`);
console.log (bites.toExponential().replace(/e\+/g, '^'));
Upvotes: 1