Limit decimal to two decimal places - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Limit decimal to two decimal places

shinsnakeshinsnake Posts: 93Member
I'm calculating a monthly income by using this in my code:

[code]Decimal userInc = Convert.ToDecimal(sdrInc[0].ToString());
userIncMonthly += (userInc / 12);

txtMonthlyIncome.Text = "$" + userIncMonthly.ToString();[/code]

It displays a large amount of digits behind the decimal place. The way I've been removing those is this way:

[code]txtMonthlyIncome.Text = txtMonthlyIncome.Text.Remove(8, txtMonthlyIncome.Text.Length - 8);[/code]

This is assuming that the number is in the format $XXXX.XX. Now, I want to find the decimal place and then delete everything after 2 digits past the decimal place. So I would like my code to do this:

[code]txtMonthlyIncome.Text = txtMonthlyIncome.Text.Remove([decimal place index]+2, txtMonthlyIncome.Text.Length - [decimal place index] + 2);[/code]

What's the easiest way to do that or is there a way I can make my calculation not allow more than two decimal places?

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    [b][red]This message was edited by iwilld0it at 2006-8-21 8:59:7[/red][/b][hr]
    Try ...

    [code]
    txtMonthlyIncome.Text = String.Format("{0:c}", userIncMonthly)
    [/code]

    : I'm calculating a monthly income by using this in my code:
    :
    : [code]Decimal userInc = Convert.ToDecimal(sdrInc[0].ToString());
    : userIncMonthly += (userInc / 12);
    :
    : txtMonthlyIncome.Text = "$" + userIncMonthly.ToString();[/code]
    :
    : It displays a large amount of digits behind the decimal place. The way I've been removing those is this way:
    :
    : [code]txtMonthlyIncome.Text = txtMonthlyIncome.Text.Remove(8, txtMonthlyIncome.Text.Length - 8);[/code]
    :
    : This is assuming that the number is in the format $XXXX.XX. Now, I want to find the decimal place and then delete everything after 2 digits past the decimal place. So I would like my code to do this:
    :
    : [code]txtMonthlyIncome.Text = txtMonthlyIncome.Text.Remove([decimal place index]+2, txtMonthlyIncome.Text.Length - [decimal place index] + 2);[/code]
    :
    : What's the easiest way to do that or is there a way I can make my calculation not allow more than two decimal places?
    :



  • shinsnakeshinsnake Posts: 93Member
    Worked like a champ. Thanks.


    : [b][red]This message was edited by iwilld0it at 2006-8-21 8:59:7[/red][/b][hr]
    : Try ...
    :
    : [code]
    : txtMonthlyIncome.Text = String.Format("{0:c}", userIncMonthly)
    : [/code]
    :

Sign In or Register to comment.