Está en la página 1de 4

si el numero es mayor o igual 1000 y si el numero es menor o igual a 9999

entonces poner un punto en tercer numero de derecha a izquierda

1000
10000
100000

codigo para poner separador de miles a un cifra:

Formatear con separadores de miles una cadena


Hola, ultimamente, he visto esta pregunta tanto en foros de EEUU como de
HispanoAmerica, por ello me decido a colgar este peque�o ejemplo en C#, tambien lo
disponible en VB por si alguien lo necesita:

Para que funcione correctamente ha de invocarse a la funcion desde el evento KeyUP

CODIGO C#

public string Puntos(string strValor, int intNumDecimales)


{

string strAux = null;


string strComas = null;
string strPuntos = null;
int intX = 0;
bool bolMenos = false;

strComas = "";
if (strValor.Length == 0) return "";
strValor =
strValor.Replace(Application.CurrentCulture.NumberFormat.NumberGroupSeparator, "");
if
(strValor.Contains(Application.CurrentCulture.NumberFormat.NumberDecimalSeparator))
{
strAux = strValor.Substring(0,
strValor.LastIndexOf(Application.CurrentCulture.NumberFormat.NumberDecimalSeparator
));
strComas =
strValor.Substring(strValor.LastIndexOf(Application.CurrentCulture.NumberFormat.Num
berDecimalSeparator) + 1);
}
else {
strAux = strValor;
}

if (strAux.Substring(0, 1) ==
Application.CurrentCulture.NumberFormat.NegativeSign) {
bolMenos = true;
strAux = strAux.Substring(1);
}

strPuntos = strAux;
strAux = "";
while (strPuntos.Length > 3) {
strAux = Application.CurrentCulture.NumberFormat.NumberGroupSeparator +
strPuntos.Substring(strPuntos.Length - 3, 3) + strAux;
strPuntos = strPuntos.Substring(0, strPuntos.Length - 3);
}
if (intNumDecimales > 0) {
if
(strValor.Contains(Application.CurrentCulture.NumberFormat.PercentDecimalSeparator)
) {
strComas =
Application.CurrentCulture.NumberFormat.PercentDecimalSeparator +
strValor.Substring(strValor.LastIndexOf(Application.CurrentCulture.NumberFormat.Per
centDecimalSeparator) + 1);
if (strComas.Length > intNumDecimales) {
strComas = strComas.Substring(0, intNumDecimales + 1);
}

}
}
strAux = strPuntos + strAux + strComas;

return strAux;
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)


{
textBox1.Text = Puntos(textBox1.Text, 0);
textBox1.Select(textBox1.TextLength, 0);
}

CODIGO VB:

Public Function Puntos(ByVal strValor As String, ByVal intNumDecimales As


Integer) As String

Dim strAux As String = ""


Dim strComas As String = ""
Dim strPuntos As String = ""
Dim intX As Integer = 0
Dim bolMenos As Boolean = False

If strValor.Length = 0 Then
Return ""
End If
strValor =
strValor.Replace(Application.CurrentCulture.NumberFormat.NumberGroupSeparator, "")
If
strValor.Contains(Application.CurrentCulture.NumberFormat.NumberDecimalSeparator)
Then
strAux = strValor.Substring(0,
strValor.LastIndexOf(Application.CurrentCulture.NumberFormat.NumberDecimalSeparator
))
strComas =
strValor.Substring(strValor.LastIndexOf(Application.CurrentCulture.NumberFormat.Num
berDecimalSeparator) + 1)
Else
strAux = strValor
End If

If strAux.Substring(0, 1) =
Application.CurrentCulture.NumberFormat.NegativeSign Then
bolMenos = True
strAux = strAux.Substring(1)
End If

strPuntos = strAux
strAux = ""
While strPuntos.Length > 3
strAux = Application.CurrentCulture.NumberFormat.NumberGroupSeparator +
strPuntos.Substring(strPuntos.Length - 3, 3) + strAux
strPuntos = strPuntos.Substring(0, strPuntos.Length - 3)
End While
If intNumDecimales > 0 Then
If
strValor.Contains(Application.CurrentCulture.NumberFormat.PercentDecimalSeparator)
Then
strComas =
Application.CurrentCulture.NumberFormat.PercentDecimalSeparator +
strValor.Substring(strValor.LastIndexOf(Application.CurrentCulture.NumberFormat.Per
centDecimalSeparator) + 1)
If strComas.Length > intNumDecimales Then
strComas = strComas.Substring(0, intNumDecimales + 1)

End If
End If
End If
strAux = strPuntos + strAux + strComas

Return strAux
End Function

Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
TextBox1.Text = Puntos(TextBox1.Text, 2)
TextBox1.Select(TextBox1.TextLength, 0)
End Sub

Para evitar, que metan en la caja de texto mas de un caracter decimal:

c#

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)


{
if
(textBox1.Text.Contains(Application.CurrentCulture.NumberFormat.NumberDecimalSepara
tor) && (e.KeyChar ==
Application.CurrentCulture.NumberFormat.NumberDecimalSeparator))
e.Handled = true;
else
e.Handled = false;
}

VB

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If
TextBox1.Text.Contains(Application.CurrentCulture.NumberFormat.PercentDecimalSepara
tor) And (e.KeyChar =
Application.CurrentCulture.NumberFormat.PercentDecimalSeparator) Then
e.Handled = True
Else
e.Handled = False
End If
End Sub

para invocar evento

textBox1.KeyUp += TextBox1_KeyUp;
private void TextBox1_KeyUp(object sender, KeyEventArgs e)
{
textBox2.AppendText( $"KeyUp code: {e.KeyCode}, value: {e.KeyValue},
modifiers: {e.Modifiers}" + "\r\n");
}

También podría gustarte