Created
January 30, 2024 22:21
-
-
Save youssef3wi/5be54a6b1de70f90f094dae2b99ca8c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Pour éviter la confusion entre les multiples de 1000 octets et les multiples de | |
| * 1024 octets, les terms "kibioctet", "mébioctet", etc. ont été inventés. | |
| * <ul> | |
| * <li>Un kibioctet (abrégé KiB) correspond à 1024 octets.</li> | |
| * <li>Un mébioctet (MiB) correspond à (1024*1024 = 1048576) octets</li> | |
| * </ul> | |
| * <p> | |
| * Étant donné une quantité d'octets : | |
| * <ul> | |
| * <li>Si elle est inférieure à un KiB, renvoyez-la sous forme d'une chaîne de caractère.</li> | |
| * <li>Si elle est comprise entre un KiB (inclus) et un MiB (exlu), convertisserz-la | |
| * en KiB, arrondissez à l'unité inférieure et renvoyez-la suivie d'un espace et du | |
| * texte "KiB".</li> | |
| * <li>Si elle est supérieure ou égale à un MiB, convertissez-la en MiB, arrondissez | |
| * à l'unité inférieure et renvoyez-la suivie d'un espace et du texte "MiB".</li> | |
| * </ul> | |
| * Vous n'aurez jamais de valeurs supérieures à 10^9 octets, donc vous n'atteindrez | |
| * jamais le "gibibyte". | |
| * <p> | |
| * Voir l'exemple ci-dessous. | |
| * <hr width="200px" /> | |
| * - Contraintes | |
| * <ul> | |
| * <li><mark>bytesQuantity</mark> <= 1 000 000 000</li> | |
| * <li>Mémoire RAM disponible : 512 Mo</li> | |
| * </ul> | |
| * - Exemple | |
| * <table border="1"> | |
| * <tr> | |
| * <td>Paramètres</td> | |
| * <td>Valeur de retour</td> | |
| * </tr> | |
| * <tr> | |
| * <td>500000</td> | |
| * <td>488 KiB</td> | |
| * </tr> | |
| * </table> | |
| */ | |
| public class ByteUnit { | |
| /** | |
| * @param bytesQuantity une quantité, exprimée en octets. | |
| * @return la même quantité, exprimée en kibioctets (KiB) ou en mébioctets (MiB), selon son ordre de grandeur. | |
| */ | |
| public static String compute(int bytesQuantity) { | |
| if (bytesQuantity >= 1024) { | |
| String unit = " MiB"; | |
| int dividend = 1024 * 1024; | |
| if (bytesQuantity < (1024 * 1024)) { | |
| unit = " KiB"; | |
| dividend = 1024; | |
| } | |
| return ((int) bytesQuantity / dividend) + unit; | |
| } | |
| return bytesQuantity + ""; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment