Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.