Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions DFe.Classes/Valor.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Globalization;
using System;
using System.Globalization;

namespace DFe.Classes
{
public static class Valor
{
public static decimal Arredondar(this decimal valor, int casasDecimais)
{
var valorNovo = decimal.Round(valor, casasDecimais);
var valorNovoStr = valorNovo.ToString("F" + casasDecimais, CultureInfo.CurrentCulture);
return decimal.Parse(valorNovoStr);
var valorArredondado = decimal.Round(valor, casasDecimais, MidpointRounding.ToEven);
var valorArredondadoFormatado = valorArredondado.ToString("F" + casasDecimais, CultureInfo.CurrentCulture);
return decimal.Parse(valorArredondadoFormatado);
}

public static decimal? Arredondar(this decimal? valor, int casasDecimais)
Expand Down
23 changes: 23 additions & 0 deletions DFe.Testes/Valores/DadosDeTeste/ValorDadosDeTeste.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;

namespace DFe.Testes.Valores.DadosDeTeste
{
public class ValorDadosDeTeste
{
public static IEnumerable<object[]> ObterValoresDecimaisParaArredondar()
{
return new List<object[]>
{
new object[] { 20.35m * 15.90m, 2 },
new object[] { 0.35m * 15.90m, 2 },
new object[] { 3.665m, 2 },
new object[] { 4.775m, 2 },

new object[] { 20.35m * 15.90m, 3 },
new object[] { 0.35m * 15.90m, 3 },
new object[] { 4.77575m, 4 },
new object[] { 5.445545m, 5 },
};
}
}
}
20 changes: 20 additions & 0 deletions DFe.Testes/Valores/ValorTesteUnitario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using DFe.Testes.Valores.DadosDeTeste;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NFe.Classes;

namespace DFe.Testes.Valores
{
[TestClass]
public class ValorTesteUnitario
{
[TestMethod(displayName: "Dado um valor e uma quantidade de casas decimais, quando o arredondamento for feito utilizando os métodos da DFe e NFe, então o valor arredondado deve ser igual em ambos os casos")]
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresDecimaisParaArredondar), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
public void DadoUmValorEUmaQuantidadeDeCasasDecimaisQuandoOArredondamentoForFeitoUtilizandoOsMetodosDaDfeENfeEntaoOValorArredondadoDeveSerIgualEmAmbosOsCasos(decimal valor, int casasDecimais)
{
var valorArredondadoDfe = Classes.Valor.Arredondar(valor, casasDecimais);
var valorArredondadoNfe = Valor.Arredondar(valor, casasDecimais);

Assert.AreEqual(valorArredondadoDfe, valorArredondadoNfe);
}
}
}
6 changes: 3 additions & 3 deletions NFe.Classes/Valor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public static class Valor
{
public static decimal Arredondar(this decimal valor, int casasDecimais)
{
var valorNovo = decimal.Round(valor, casasDecimais, MidpointRounding.AwayFromZero);
var valorNovoStr = valorNovo.ToString("F" + casasDecimais, CultureInfo.CurrentCulture);
return decimal.Parse(valorNovoStr);
var valorArredondado = decimal.Round(valor, casasDecimais, MidpointRounding.ToEven);
var valorArredondadoFormatado = valorArredondado.ToString("F" + casasDecimais, CultureInfo.CurrentCulture);
return decimal.Parse(valorArredondadoFormatado);
}

public static decimal? Arredondar(this decimal? valor, int casasDecimais)
Expand Down
Loading