The amount of gb mb kb and bytes in X bytes, java -



The amount of gb mb kb and bytes in X bytes, java -

ok have problem pg class, i'm supposed take number(which represents number of bytes) input , convert gigabytes, megabytes, kilobytes , bytes. did little research , couldn't find way solve this. i'm using java way. in advice!

heres example: // 3434901120028 - input // 3199 gb + 1001 kb + 28 bytes - output

the number of bytes divided 1024 number of kilobytes. number of kilobytes divided 1024 number of megabytes. divided 1024 number of gigabytes. like,

long bytes = 3434901120028l; long gb = bytes / (1024 * 1024 * 1024); bytes -= gb * 1024 * 1024 * 1024; long mb = bytes / (1024 * 1024); bytes -= mb * 1024 * 1024; long kb = bytes / 1024; bytes -= kb * 1024; if (gb > 0) { system.out.printf("%d gigabytes ", gb); } if (mb > 0) { system.out.printf("%d megabytes ", mb); } if (kb > 0) { system.out.printf("%d kilobytes ", kb); } if (bytes > 0) { system.out.printf("%d bytes ", bytes); }

output is

3199 gigabytes 1001 kilobytes 28 bytes

java byte converter

Comments

Popular posts from this blog

javascript - THREE.js reposition vertices for RingGeometry -

javascript - I need to update the text of a paragraph by inline edit -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -