Convert Word (Thousand and Million) to Number using C# and VB.Net in ASP.Net

Last Reply 28 days ago By arunkurmi

Posted 28 days ago

hello,

i have some values in textbox which has written like

1.02M

76.4K

but while i storing in database i want to conver it like 76.4K means 76400 and 1.02M means 1000002

please advice to convert M and K to its actual values 

You are viewing reply posted by: arunkurmi 28 days ago.
Posted 28 days ago

Hi nauna,

Refer Below code.

HTML

Value:<asp:TextBox runat="server" ID="txtValue" />
<br /><br />
<asp:Button Text="Submit" runat="server" OnClick="Submit"/>

Code

C#

protected void Submit(object sender, EventArgs e)
{
    if (txtValue.Text.Trim().EndsWith("M"))
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + Math.Round((Convert.ToDecimal(txtValue.Text.TrimEnd('M')) * 1000000), 0) + "')", true);
    }
    else if (txtValue.Text.Trim().EndsWith("K"))
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + Math.Round((Convert.ToDecimal(txtValue.Text.TrimEnd('K')) * 1000), 0) + "')", true);
    }
}

VB.Net

Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
    If txtValue.Text.Trim().EndsWith("M") Then
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('" & Math.Round((Convert.ToDecimal(txtValue.Text.TrimEnd("M"c)) * 1000000), 0) & "')", True)
    ElseIf txtValue.Text.Trim().EndsWith("K") Then
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('" & Math.Round((Convert.ToDecimal(txtValue.Text.TrimEnd("K"c)) * 1000), 0) & "')", True)
    End If
End Sub

Screenshot