ARREDONDAR VALOR NA ULTIMA PARCELA

JOHNSTEVE 28/10/2016 18:30:13
#468528
tudo Joinha pessoal??

Bom, hj me deparei com um problema. qnd gero minhas parcelas q o valor sao quebrados. minha funçao esta arredondando em cada parcela.

vejam no print como ficou..

essa e minha funçao de gerar as parcelas.
  Private Sub GeraParcelas_Grid()
If txtValor.Text <> [Ô][Ô] Then

Dim Venc As DateTime
Venc = FormatDateTime(mkbData.Text, DateFormat.ShortDate)
Venc = Venc.AddDays(30)
For i = 0 To Val(txtParcelas.Text) - 1

Venc = Venc.AddDays(i * 30)

dgvFormapagto.Rows.Add(i + 1, Format(CDbl(Me.txtTotal.Text), [Ô]###,##0.00[Ô]) / Val(txtParcelas.Text), Mid(Venc.ToString, 1, 10), cbo_tipoNFe.Text)

Next

End If
End Sub


[txt-color=#e80000] Por favor, alguem pode me ajudar como faço para arredondar apenas na ultima parcela.
para que o valor das parcelas bate o valor total da venda.
pois nessa minha funçao esta dando diferença..
as vezes da diferença de 0,1 centavo, 0,2 centavo..etc.. as diferença sao de centavos..
mais na da certo assim nao pode ficar assim com essa diferença. [/txt-color]
PLUGSOFTSM 28/10/2016 22:50:31
#468536
Pode fazer assim

Private Sub GeraParcelas_Grid()
If txtValor.Text <> [Ô][Ô] Then
Dim Venc As DateTime
Venc = FormatDateTime(mkbData.Text, DateFormat.ShortDate)
Venc = Venc.AddDays(30)
Dim Total as single = 0
dim TotalPrcela as single = Cdbl( format(Format(CDbl(Me.txtTotal.Text), [Ô]###,##0.00[Ô]) / Val(txtParcelas.Text), Val(txtParcelas.Text), [Ô]###,##0.00[Ô]) )
For i = 0 To Val(txtParcelas.Text) - 1

Venc = Venc.AddDays(i * 30)
[ô]Verifico se é a última parcela
If I = val(TxtParcelas.text)- 1 then
TotalParcela = CDbl(Me.txtTotal.Text) - Total
else
[ô]Acumulo o valor das parcelas
Total+= TotalParcela
endif

dgvFormapagto.Rows.Add(i + 1, Format(TotalParcela, [Ô]###,##0.00[Ô]) , Mid(Venc.ToString, 1, 10), cbo_tipoNFe.Text)

Next

End If
End Sub
JOHNSTEVE 28/10/2016 23:15:42
#468540
Citação:

:
Pode fazer assim

Private Sub GeraParcelas_Grid()
If txtValor.Text <> [Ô][Ô] Then
Dim Venc As DateTime
Venc = FormatDateTime(mkbData.Text, DateFormat.ShortDate)
Venc = Venc.AddDays(30)
Dim Total as single = 0
dim TotalPrcela as single = Cdbl( format(Format(CDbl(Me.txtTotal.Text), [Ô]###,##0.00[Ô]) / Val(txtParcelas.Text), Val(txtParcelas.Text), [Ô]###,##0.00[Ô]) )
For i = 0 To Val(txtParcelas.Text) - 1

Venc = Venc.AddDays(i * 30)
[ô]Verifico se é a última parcela
If I = val(TxtParcelas.text)- 1 then
TotalParcela = CDbl(Me.txtTotal.Text) - Total
else
[ô]Acumulo o valor das parcelas
Total+= TotalParcela
endif

dgvFormapagto.Rows.Add(i + 1, Format(TotalParcela, [Ô]###,##0.00[Ô]) , Mid(Venc.ToString, 1, 10), cbo_tipoNFe.Text)

Next

End If
End Sub




PLUGSOFTSM, nao deu certo.. ele ta fazendo assim
valor total: 80,00
ai se eu divido em 5
ta ficando assim
[txt-color=#e80000] 0,00
0,00
0,00
0,00
80,00 [/txt-color]
JOHNSTEVE 28/10/2016 23:17:11
#468541
Nilson ????

como seria usando o operador Mod ?


NILSONTRES 28/10/2016 23:28:46
#468542
Resposta escolhida
Eu tinha te passado o Mod, mas a sobra pode ser muito grande, fui ver como eu faço, nem me lembrava.
Faço isso, identifico se existe diferença entre as parcelas e o valor total, havendo diferença, qual o valor da diferença ?. no seu caso ai 0,1 centavo.
Se for a menor acrescento na ultima parcela, se for a maior desconto a ultima parcela.
8,67
8,67
8,66
JOHNSTEVE 28/10/2016 23:32:27
#468543
Citação:

:
Eu tinha te passado o Mod, mas a sobra pode ser muito grande, fui ver como eu faço, nem me lembrava.
Faço isso, identifico se existe diferença entre as parcelas e o valor total, havendo diferença, qual o valor da diferença ?. no seu caso ai 0,1 centavo.
Se for a menor acrescento na ultima parcela, se for a maior desconto a ultima parcela.



showw :)
ai nilson vc tem a funçao para acrescentar na ultima parcela ou descontar ?
NILSONTRES 29/10/2016 00:25:30
#468544
Não sei se vai te ajudar, tem muitas particularidades.
Private Function DUPLICATAS(ByVal V_AT_VENDA As String) As Boolean
Try
Me.GRID_DPL.Rows.Clear()
Dim V_SOMA As Double = [Ô]0,00[Ô]
Dim V_SOMAFINAL As Double = [Ô]0,00[Ô]
[ô]**CARREGA AS PARCELAS DA TABELA
If SQL_NAG.SEL([Ô]Select *FROM nfe_dpls WHERE AT_VENDA=[Ô] & V_AT_VENDA & [Ô] ORDER BY ITEN[Ô], False) = True Then
While SQL_NAG.MY_RE.Read
With GRID_DPL
.Rows.Add()
.Rows(.Rows.Count - 1).Cells(GDPL_ITEM.Name).Value = SQL_NAG.MY_RE.Item([Ô]ITEN[Ô]).ToString
.Rows(.Rows.Count - 1).Cells(GDPL_NUM.Name).Value = SQL_NAG.MY_RE.Item([Ô]NUMERO[Ô]).ToString
.Rows(.Rows.Count - 1).Cells(GDPL_VENC.Name).Value = Format(CType(SQL_NAG.MY_RE.Item([Ô]DATAVC[Ô]).ToString, Date), [Ô]dd/MM/yyyy[Ô])
.Rows(.Rows.Count - 1).Cells(GDPL_VALOR.Name).Value = FormatNumber(SQL_NAG.MY_RE.Item([Ô]VALORDPL[Ô]).ToString, 2)
End With
V_SOMA = V_SOMA + SQL_NAG.MY_RE.Item([Ô]VALORDPL[Ô]).ToString
End While
SQL_NAG.FECHA_SEL()

[ô]**VERIFICA SE TOTAL DAS DPLS é DIFERENTE DO TOTAL DA NOTA, PODE SER QUE ENTROU FRETE OU OUTROS.
If FormatNumber(V_SOMA, 2) <> TXT_TOTNFE.Text Then [ô]DIFERENTE, RECALCULA OS VALORES
Dim Recalcula As Double = 0
Dim DividiValores As Double = FormatNumber(TXT_TOTNFE.Text / GRID_DPL.Rows.Count, 2) [ô]DIVIDE O TOTAL PELA QUANTIDADE DE PARCELAS
Dim ii As Integer
For ii = 0 To GRID_DPL.Rows.Count - 1
With GRID_DPL
.Rows(ii).Cells(GDPL_VALOR.Name).Value = FormatNumber(DividiValores, 2)
Recalcula = Recalcula + .Rows(ii).Cells(GDPL_VALOR.Name).Value
End With
Next

[ô]**RECALCULA CASO HAJA RESTO NA DIVISÃO DAS PARCELAS, JOGA O RESTO PARA A ULTIMA PARCELA, UTILIZANDO O OPERADOR MOD.
If FormatNumber(Recalcula, 2) <> TXT_TOTNFE.Text Then

Dim i As Integer
Dim Divisao As Double = FormatNumber(CLng(TXT_TOTNFE.Text / GRID_DPL.Rows.Count), 2)
Dim vmod As Double = FormatNumber(TXT_TOTNFE.Text Mod GRID_DPL.Rows.Count, 2)
For i = 0 To GRID_DPL.Rows.Count - 1
With GRID_DPL
.Rows(i).Cells(GDPL_VALOR.Name).Value = FormatNumber(Divisao, 2)
If i = GRID_DPL.Rows.Count - 1 Then
.Rows(i).Cells(GDPL_VALOR.Name).Value = FormatNumber(Divisao + vmod, 2)
End If
V_SOMAFINAL = V_SOMAFINAL + .Rows(i).Cells(GDPL_VALOR.Name).Value
End With
Next
Me.LBL_SOMA_DPL.Text = FormatNumber(V_SOMAFINAL, 2)
End If
End If

Me.GRID_DPL.Rows.Add([Ô][Ô], [Ô][Ô], [Ô] R$ TOTAL:[Ô], FormatNumber(V_SOMA, 2))
Dim V_RODAPE As New Classe_Teste
V_RODAPE.RODAPE(GRID_DPL, GRID_DPL.Rows.Count - 1, 0)
Me.LBL_SOMA_DPL.Text = FormatNumber(V_SOMA, 2)
[ô] GRID_DPL.Rows.Add()

End If
Return True
Catch
MsgBox(Err.Description & vbCrLf & Err.GetException.StackTrace, MsgBoxStyle.Critical, [Ô]DUPLICATAS[Ô])
Return False
End Try
End Function
JOHNSTEVE 29/10/2016 15:38:58
#468559
Nilson, achei a diferença agora só falta obter essa diferença na ultima parcela ou na primeira..
como faço para somar essa diferença na ultima parcela ou na primeira.. ja rodei na net. e nao achei nada relacionado a obter a diferenca na primeira ou na ultima parcela..

veja

  Private Sub Encontrar_Diferenca()
Dim soma As Double
For i As Integer = 0 To dgvFormapagto.Rows.Count - 1

soma += dgvFormapagto.Rows(i).Cells(1).Value.ToString()
Next

Dim diferenca As Double = 0
Dim somar1 As Double
somar1 = Format(CDbl(soma))
If somar1 > txtTotal.Text Then
diferenca = somar1 - txtTotal.Text
somar1 = False
Else
diferenca = txtTotal.Text - Format(CDbl(somar1))
End If

If diferenca > 0 Then
Dim AddDiferenca As Decimal
[ô]Round((CDbl(txtTotalProdutos.Text) * AliquotaInter) / 100, 2)
AddDiferenca = Round((CDbl(diferenca)), 2)

MsgBox([Ô] A diferença e essa -> [Ô] & AddDiferenca)

End If
End Sub


nesse exemplo ai. foi utilizadoo valor total : [txt-color=#e80000] 64,00 [/txt-color] dividido por 3
no datagrid ficou assim inserido.
6 x 10,67 = 64,02

eita porra tem algo errado... no caso ta arredondando todas as parcelas... e por isso deu um aumento de 0,02.
no caso ultrapassou o valor total da venda . q foi 64,00
PLUGSOFTSM 30/10/2016 09:39:26
#468579
Refiz a rotina
Essa aqui tá testada e certinha

Private Sub GeraParcelas_Grid()
If TxtTotal.Text <> [Ô][Ô] Then
Dim Venc As DateTime
Venc = FormatDateTime(mkbData.Text, DateFormat.ShortDate)
Venc = Venc.AddDays(30)
Dim Total As Single = 0
Dim TotalParcela As Single = Math.Round(CDbl(TxtTotal.Text) / Val(TxtParcelas.Text), 2)
For i = 0 To Val(txtParcelas.Text) - 1
Venc = Venc.AddDays(i * 30)
[ô]Verifico se é a última parcela
If I = val(TxtParcelas.text) - 1 Then
TotalParcela = CDbl(Me.txtTotal.Text) - Total
Else
[ô]Acumulo o valor das parcelas
Total += TotalParcela
End If
dgvFormapagto.Rows.Add(i + 1, Format(TotalParcela, [Ô]###,##0.00[Ô]), Mid(Venc.ToString, 1, 10), cbo_tipoNFe.Text)
Next

End If
End Sub

Essa função acima vai recalcular as parcelas de modos que o centavo para mais ou para menos seja compensado na última parcela
Mas vc deve tomar o cuidado com os centavos. Imagine a seguinte situação:
Duas parcelas de 33,33 e uma de 33,34 (Duvido de seu cliente não queira fazer 3 parcelas de 33,00 e outra de 34,00)
Se for isso aí sim vc poderia usar o operador mod

Uma solução seria assim
Totalparcela = Total\Parcelas
Resto = Total Mod Parcelas

A última parcela ficaria TotalParcela + Resto

Essa seria apenas umas de muitas soluções
JOHNSTEVE 30/10/2016 18:33:34
#468586
Citação:

:
Refiz a rotina
Essa aqui tá testada e certinha

Private Sub GeraParcelas_Grid()
If TxtTotal.Text <> [Ô][Ô] Then
Dim Venc As DateTime
Venc = FormatDateTime(mkbData.Text, DateFormat.ShortDate)
Venc = Venc.AddDays(30)
Dim Total As Single = 0
Dim TotalParcela As Single = Math.Round(CDbl(TxtTotal.Text) / Val(TxtParcelas.Text), 2)
For i = 0 To Val(txtParcelas.Text) - 1
Venc = Venc.AddDays(i * 30)
[ô]Verifico se é a última parcela
If I = val(TxtParcelas.text) - 1 Then
TotalParcela = CDbl(Me.txtTotal.Text) - Total
Else
[ô]Acumulo o valor das parcelas
Total += TotalParcela
End If
dgvFormapagto.Rows.Add(i + 1, Format(TotalParcela, [Ô]###,##0.00[Ô]), Mid(Venc.ToString, 1, 10), cbo_tipoNFe.Text)
Next

End If
End Sub

Essa função acima vai recalcular as parcelas de modos que o centavo para mais ou para menos seja compensado na última parcela
Mas vc deve tomar o cuidado com os centavos. Imagine a seguinte situação:
Duas parcelas de 33,33 e uma de 33,34 (Duvido de seu cliente não queira fazer 3 parcelas de 33,00 e outra de 34,00)
Se for isso aí sim vc poderia usar o operador mod

Uma solução seria assim
Totalparcela = TotalParcelas
Resto = Total Mod Parcelas

A última parcela ficaria TotalParcela + Resto

Essa seria apenas umas de muitas soluções




PLUGSOFTSM, deu certo, agora so tive um problema com a data.
veja no print abaixo. do mes [txt-color=#e80000] 02 [/txt-color] pulou para [txt-color=#e80000] 05 [/txt-color]
nao faz sentido.. kkkkk
JOHNSTEVE 30/10/2016 18:35:26
#468587
aqui o print...
Ficou foi doido kkkkkkkkkk.... gerou ate 2018 ta com a porrra. kkkkkkkkkkkkkkk

Página 1 de 2 [18 registro(s)]
Tópico encerrado , respostas não são mais permitidas