| | 1 | | @using System.Text |
| | 2 | | @inject IJSRuntime JSRuntime |
| | 3 | |
|
| | 4 | | <div class="container"> |
| | 5 | |
|
| | 6 | | <div class="row"> |
| | 7 | | <div class="col"> |
| | 8 | | <h2>Memory Converter</h2> |
| | 9 | | </div> |
| | 10 | | </div> |
| | 11 | |
|
| | 12 | | <div class="row"> |
| | 13 | | <div class="col"> |
| | 14 | | <div class="input-group"> |
| | 15 | | <div class="input-group-prepend"> |
| | 16 | | <span class="input-group-text">Bytes:</span> |
| | 17 | | </div> |
| | 18 | | <input type="number" id="input" class="form-control" @bind="Input" placeholder="1030" /> |
| | 19 | | <span class="input-group-btn"> |
| | 20 | | <button id="btnClear" name="btnBinaryClear" class="btn btn-danger float-right" @onclick="Clear"><i c |
| | 21 | | </span> |
| | 22 | | </div> |
| | 23 | | </div> |
| | 24 | | </div> |
| | 25 | |
|
| | 26 | | <div class="row"> |
| | 27 | | <div class="col"> |
| | 28 | | <button id="btnConvert" name="btnConvert" class="btn btn-success float-right" @onclick="Convert">Convert</bu |
| | 29 | | </div> |
| | 30 | | </div> |
| | 31 | |
|
| | 32 | | <div class="row"> |
| | 33 | | <div class="col"> |
| | 34 | | <div class="input-group"> |
| | 35 | | <input id="output" class="form-control" @bind="Output" placeholder="KB / MB / GB / TB / PB / EB / ZB / Y |
| | 36 | | <span class="input-group-btn"> |
| | 37 | | <button id="btnCopy" name="btnCopy" class="btn btn-info float-right" @onclick="Copy"><i class="far f |
| | 38 | | </span> |
| | 39 | | </div> |
| | 40 | |
|
| | 41 | | </div> |
| | 42 | | </div> |
| | 43 | |
|
| | 44 | | </div> |
| | 45 | |
|
| | 46 | | @code { |
| | 47 | | [Parameter] |
| 7 | 48 | | public long Input { get; set; } |
| | 49 | |
|
| | 50 | | [Parameter] |
| 7 | 51 | | public string? Output { get; set; } |
| | 52 | |
|
| | 53 | | protected override void OnInitialized() |
| 1 | 54 | | { |
| 1 | 55 | | Input = 1030; |
| 1 | 56 | | Output = string.Empty; |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | void Convert() |
| 1 | 60 | | { |
| 1 | 61 | | var data = SizeSuffix(Input); |
| 1 | 62 | | Output = data; |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | private void Clear() |
| 0 | 66 | | { |
| 0 | 67 | | Input = 0; |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | async Task Copy() |
| 0 | 71 | | { |
| 0 | 72 | | await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", Output); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | #region Convert |
| | 76 | | // https://stackoverflow.com/a/14488941/2895831 |
| 1 | 77 | | static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; |
| | 78 | |
|
| | 79 | | static string SizeSuffix(Int64 value, int decimalPlaces = 1) |
| 1 | 80 | | { |
| 1 | 81 | | if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); } |
| 1 | 82 | | if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); } |
| 1 | 83 | | if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); } |
| | 84 | |
|
| | 85 | | // mag is 0 for bytes, 1 for KB, 2, for MB, etc. |
| 1 | 86 | | int mag = (int)Math.Log(value, 1024); |
| | 87 | |
|
| | 88 | | // 1L << (mag * 10) == 2 ^ (10 * mag) |
| | 89 | | // [i.e. the number of bytes in the unit corresponding to mag] |
| 1 | 90 | | decimal adjustedSize = (decimal)value / (1L << (mag * 10)); |
| | 91 | |
|
| | 92 | | // make adjustment when the value is large enough that |
| | 93 | | // it would round up to 1000 or more |
| 1 | 94 | | if (Math.Round(adjustedSize, decimalPlaces) >= 1000) |
| 0 | 95 | | { |
| 0 | 96 | | mag += 1; |
| 0 | 97 | | adjustedSize /= 1024; |
| 0 | 98 | | } |
| | 99 | |
|
| 1 | 100 | | return string.Format("{0:n" + decimalPlaces + "} {1}", |
| 1 | 101 | | adjustedSize, |
| 1 | 102 | | SizeSuffixes[mag]); |
| 1 | 103 | | } |
| | 104 | | #endregion Convert |
| | 105 | | } |