1

Hi guys I have bytes say 007458415820874584158208042423283712.I want to convert this into GB, so tried to divide it by 1048576 i am getting a result of 7.112899609446129e+27. I want only the two numbers after the decimal point, so I have used .toFixed like below. It doesn't work, I am getting the same response as if I have not used the toFixed function. I just want the result to be just 7.1. help me out on this.

   console.log((007458415820874584158208042423283712/1048576).toFixed(2));
Not A Bot
  • 2,474
  • 2
  • 16
  • 33
yasarui
  • 6,209
  • 8
  • 41
  • 75
  • 3
    Of course not: `toFixed` is for controlling how many digits after the decimal point you get. There is no "decimal point" in 7.112899609446129e+27, that "e+27" means "times 1000000000000000000000000000". The number you're showing is 7-with-27-zeroes gigabytes. So, what do you _actually_ need? Because a number that starts with zeroes, like you're showing, suggests this isn't actually something you need to do and you made something up - can you describe the _actual_ use case? – Mike 'Pomax' Kamermans Oct 16 '20 at 05:05
  • Does this answer your question.? https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript – Dipankar Maikap Oct 16 '20 at 05:08
  • 3
    that number is `6024860 brontobytes` or `6,946,191,024,849,735,347,747,842` Gigabytes - that's a lot ... the total world storage is estimated at 295 exabytes ... or 295,000,000,000 gigabytes ... so your number is 20432 times larger than the total estimated data currently in existence (I may be out by several orders of magnitude, I'm still laughing at brontobytes :p ) – Jaromanda X Oct 16 '20 at 05:14

2 Answers2

4

You can use this prototype function for your solution.

Number.prototype.toFixedSpecial = function(n) {
  var str = this.toFixed(n);
  if (str.indexOf('e+') === -1)
    return str;

  // if number is in scientific notation, pick (b)ase and (p)ower
  str = str.replace('.', '').split('e+').reduce(function(p, b) {
    return p + Array(b - p.length + 2).join(0);
  });
  
  if (n > 0)
    str += '.' + Array(n + 1).join(0);
  
  return str;
};
var val = (007458415820874584158208042423283712/1048576);
console.log(val);
console.log(val.toFixedSpecial(2))           //"7112899609446129000000000000.00"
console.log( 1e21.toFixedSpecial(2) );       // "1000000000000000000000.00"
console.log( 2.1e24.toFixedSpecial(0) );     // "2100000000000000000000000"
console.log( 1234567..toFixedSpecial(1) );   // "1234567.0"
console.log( 1234567.89.toFixedSpecial(3) ); // "1234567.890"
Krutika Patel
  • 420
  • 5
  • 16
3

Your problem is that this is scientific notation and toFixed() supports 20 decimal places. Your number is 7.112899609446129e+27 which technically (most likely) has decimal places but they are not visible due to scientific notation.

The solution would be to use toExponential() like so:

parseFloat((7458415820874584158208042423283712/1048576.0).toExponential(2))

Output: 7.11e+27

A more correct way is shown here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential

But this gives "7.11e+27" (a string)

If you just want 7.11 then you can use slice(0,3) as follows:

var result_str = (7458415820874584158208042423283712/1048576).toExponential(2);
console.log(parseFloat(result_str.slice(0,3)));

Result: 7.1

lbragile
  • 7,549
  • 3
  • 27
  • 64