FabioRochapt Posted August 13, 2012 at 03:57 PM Report Share #472172 Posted August 13, 2012 at 03:57 PM (edited) Olá a todos, Private Sub txtBalance_LostFocus(sender As Object, e As EventArgs) Handles txtBalance.LostFocus Dim amount As Decimal = CType(txtBalance.Text, Decimal) txtBalance.Text = String.Format("{0:n2}", amount) End Sub Estou a usar o evento lostfocus para quando o utilizar tiver inserido um valor exemplo: 1410,1363, este valor será convertido para 1410,14. O meu problema está em que isto só funciona quando introduzes VIRGULA e não PONTO, para definir as casas decimais. Alguma ideia? Obrigado. Edited August 13, 2012 at 04:00 PM by FabioRochapt Link to comment Share on other sites More sharing options...
Caça Posted August 13, 2012 at 04:07 PM Report Share #472173 Posted August 13, 2012 at 04:07 PM Utiliza o Decimal.Parse http://msdn.microsoft.com/pt-br/library/s84kdbzx(v=vs.90).aspx 1 Report Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
FabioRochapt Posted August 13, 2012 at 04:23 PM Author Report Share #472174 Posted August 13, 2012 at 04:23 PM Private Sub txtBalance_LostFocus(sender As Object, e As EventArgs) Handles txtBalance.LostFocus Try Dim Dinheiro = Decimal.Parse(txtBalance.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture) txtBalance.Text = String.Format("{0:n2}", Dinheiro) Catch txtBalance.Text = "Error" End Try End Sub Agora se usar a VIRGULA não funciona, mas para mim (por enquanto) não será um problema. Obrigado pela sugestão. Link to comment Share on other sites More sharing options...
thoga31 Posted August 13, 2012 at 07:47 PM Report Share #472192 Posted August 13, 2012 at 07:47 PM Segundo entendi pelo teu código, estás a receber um valor por uma TextBox. Assim sendo, a solução é simples 😉 Dim amount As Decimal = CType(txtBalance.Text.Replace(".", ","), Decimal) Como o que recebes é uma String e tens de converter em Decimal, substituis logo o ponto por vírgula com o método Replace e só depois se converte 😉 Knowledge is free! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now