Skip to content

Commit

Permalink
Merge pull request #10 from j4587698/feat-human
Browse files Browse the repository at this point in the history
添加LongExtension
  • Loading branch information
j4587698 authored Feb 12, 2024
2 parents 4b92377 + ac79c88 commit 1822b31
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Jx.Toolbox.Test/Jx.Toolbox.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<TargetFrameworks>net6.0;net8.0</TargetFrameworks>

<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions Jx.Toolbox.Test/LongTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Jx.Toolbox.Extensions;

namespace Jx.Toolbox.Test;

public class LongTest
{
[Fact]
public void ToSizeString_Ok()
{
int fileSizeInBytes = 12345678;
Assert.Equal("11.77 MB", fileSizeInBytes.ToSizeString());
}

[Fact]
public void ToTimeString_Ok()
{
int totalSeconds = 3661;
Assert.Equal("01:01:01", totalSeconds.ToTimeString());
}
}
76 changes: 76 additions & 0 deletions Jx.Toolbox/Extensions/LongExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;

namespace Jx.Toolbox.Extensions
{
public static class LongExtension
{
private static readonly string[] SizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };

/// <summary>
/// 转换为可读进制(B,KB,MB,GB,TB,PB,EB)并保留两位小数
/// </summary>
/// <param name="value">要转换的数字(需可以转换为long)</param>
/// <typeparam name="T"></typeparam>
/// <returns>00.00 MB(KB,GB等)的字符串</returns>
/// <exception cref="ArgumentException"></exception>
public static string ToSizeString<T>(this T value) where T : IConvertible
{
try
{
// 将值转换为 long,这是因为字节通常是整数
long byteCount = value.ToInt64(System.Globalization.CultureInfo.CurrentCulture);

if (byteCount < 0) { return "-" + ToSizeString(-byteCount); }
if (byteCount == 0) { return "0 B"; }

int i = 0;
double dValue = byteCount;
while (dValue >= 1024 && i < SizeSuffixes.Length - 1)
{
dValue /= 1024;
i++;
}

return string.Format("{0:0.##} {1}", dValue, SizeSuffixes[i]);
}
catch (OverflowException)
{
throw new ArgumentException("Value is too large to be represented as a byte count", nameof(value));
}
catch (InvalidCastException)
{
throw new ArgumentException("Value is not a recognized numeric type", nameof(value));
}
}

/// <summary>
/// 转换为00:00:00的格式
/// </summary>
/// <param name="value">要转换的数字(需可以转换为long)</param>
/// <typeparam name="T"></typeparam>
/// <returns>00:00:00格式的字符串</returns>
/// <exception cref="ArgumentException"></exception>
public static string ToTimeString<T>(this T value) where T : IConvertible
{
try
{
// 将值转换为long类型,这里假设传入的值表示的是秒数
long seconds = value.ToInt64(System.Globalization.CultureInfo.CurrentCulture);

// 使用TimeSpan从秒数创建时间间隔
TimeSpan time = TimeSpan.FromSeconds(seconds);

// 返回格式化的时间字符串
return time.ToString(@"hh\:mm\:ss");
}
catch (OverflowException)
{
throw new ArgumentException("Value is too large to be represented as seconds", nameof(value));
}
catch (InvalidCastException)
{
throw new ArgumentException("Value is not a recognized numeric type", nameof(value));
}
}
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ StringArrayExtension
字符数组.Join(分隔符); // 字符数组合并为字符串,默认分隔符为逗号
```

LongExtension
```
可转换为long的数字类型.ToSizeString(); // 转换为00.00 KB类型的字符串,保留两位小数,支持B KB MB GB TB PB EB
可转换为long的数字类型.ToTimeString(); // 转换为00:00:00格式的字符串
```

## 加密库

```
Expand Down

0 comments on commit 1822b31

Please sign in to comment.