Improve rounding - thx OmrSi :)

This commit is contained in:
niksedk 2021-11-23 14:05:45 +01:00
parent d19b259771
commit 85d0b121fa
2 changed files with 13 additions and 5 deletions

View File

@ -4,21 +4,29 @@ using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace Test.Logic.SubtitleFormats namespace Test.Logic.SubtitleFormats
{ {
[TestClass]
public class SubtitleFormatFunctionsTest public class SubtitleFormatFunctionsTest
{ {
[TestMethod] [TestMethod]
public void MillisecondsToFrames1() public void MillisecondsToFrames1()
{ {
var x = SubtitleFormat.MillisecondsToFrames(500, 25); var frames = SubtitleFormat.MillisecondsToFrames(500, 25);
Assert.AreEqual(x, 13); Assert.AreEqual(13, frames);
}
[TestMethod]
public void MillisecondsToFrames2()
{
var frames = SubtitleFormat.MillisecondsToFrames(499, 25);
Assert.AreEqual(12, frames);
} }
[TestMethod] [TestMethod]
public void FramesToMilliseconds1() public void FramesToMilliseconds1()
{ {
Configuration.Settings.General.CurrentFrameRate = 25.0; Configuration.Settings.General.CurrentFrameRate = 25.0;
var x = SubtitleFormat.FramesToMilliseconds(1); var ms = SubtitleFormat.FramesToMilliseconds(1);
Assert.AreEqual(x, 40); Assert.AreEqual(ms, 40);
} }
} }
} }

View File

@ -425,7 +425,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public static int MillisecondsToFrames(double milliseconds, double frameRate) public static int MillisecondsToFrames(double milliseconds, double frameRate)
{ {
return (int)Math.Round(milliseconds / (TimeCode.BaseUnit / GetFrameForCalculation(frameRate))); return (int)Math.Round(milliseconds / (TimeCode.BaseUnit / GetFrameForCalculation(frameRate)), MidpointRounding.AwayFromZero);
} }
public static double GetFrameForCalculation(double frameRate) public static double GetFrameForCalculation(double frameRate)