0

hi in our app we show data in GB but right now I have a really small number in bites. how can I round up my number? example: I have 0.000065gb but I really want to see 0.01gb. is there an easy way to do that?

  • Why not show Mb, b as appropriate to the file size? SOmething like this: https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript – Jamiec Sep 10 '20 at 12:47
  • because we show this number in the graph also. i don't want to refactor whole app just so i can see Mb or bite – Andrey Kishtov Sep 10 '20 at 12:50
  • 6
    `if (size < 0.01) size = 0.01;` FYI, you don't have to "refactor the whole app" to change one display value. That's a little exaggerated to say that. – jfriend00 Sep 10 '20 at 12:50

2 Answers2

2

Right before displaying the value, you can put the value in another variable and then do something like this with it:

if (size < 0.01) size = 0.01;       // set minimum display size

Or, if 0 is a legit value, so you only want to round-up non-zero things, then:

if (size !== 0 && size < 0.01) size = 0.01;       // set minimum display size
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

dividing by the precision you want to round by would work as well and would allow you to vary the precision a bit too. I found a link to an old stack overflow thread

basically something like this

Math.round( yourNumber * 100 ) / 100

He also uses Number.EPSILON since his precision specifications seem to get really close to 0.

old thread

Edit: put * in twice second should be /

tsudodog
  • 61
  • 3