dotnet / dotnet/dotnet-api-docs

TimeSpan.FromSeconds(Double) precision is inconsistent with the document

Open
#9,156 12 comments 1 reaction 0 assignees View on GitHub
area-System.DateTime documentation help wanted Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

### Description

According to the official document [TimeSpan.FromSeconds(Double)](https://learn.microsoft.com/en-us/dotnet/api/system.timespan.fromseconds?view=net-7.0):

> Returns a [TimeSpan](https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=net-7.0) that represents a specified number of seconds, where the specification is **accurate to the nearest millisecond**.

But the actual output retained more digits than millisecond.

### Reproduction Steps

```cs
double paceSpeedInSeconds = 1.5099;
Console.WriteLine(TimeSpan.FromSeconds(paceSpeedInSeconds));
Console.WriteLine(((int)TimeSpan.FromSeconds(paceSpeedInSeconds).TotalMilliseconds));
```

### Expected behavior

Should output (accurate to the nearest millisecond):
```
00:00:01.5100000
1510
```

### Actual behavior

Outputs:
```
00:00:01.5099000
1509
```

### Regression?

This is a regression. On .NET Framework 4.8, the actual behavior is consistent with the document:
```
00:00:01.5100000
1510
```

### Known Workarounds

```cs
double paceSpeedInSeconds = Math.Round(1.5099, 3);
Console.WriteLine(TimeSpan.FromSeconds(paceSpeedInSeconds));
Console.WriteLine(((int)TimeSpan.FromSeconds(paceSpeedInSeconds).TotalMilliseconds));
```

Outputs:
```
00:00:01.5100000
1510
```

### Configuration

.NET Core 3.1, .NET 5.0, .NET 6.0 on Windows.

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.