• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

Formatting a decimal so that the result always has two decimal places

The following vb.net statement calculates the payment as amount * rate.

PAYMENT.Text = "$" + Convert.ToString(Convert.ToDecimal(Convert.ToDouble(AMOUNT.Text) * Convert.ToDecimal(RATE.Text)))

and payment does not always have two trailing decimals.

I want to format payment as a currency e.g. $30.00 or $32.12.

How can I modify my statement to achieve that?
0
sib_user
Asked:
sib_user
1 Solution
 
silemoneCommented:
Payment.Text = "$"
Payment.Text += (Convert.ToString(Convert.ToDecimal(Convert.ToDouble(AMOUNT.Text) * Convert.ToDecimal(RATE.Text)))).ToString("N:2")...

0
 
sib_userAuthor Commented:
silemone,
When I try your solution, I get the error:
Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.
0
 
Alfred A.Commented:
Try this,

PAYMENT.Text = Convert.ToDecimal(Convert.ToDouble(AMOUNT.Text) * Convert.ToDecimal(RATE.Text)).ToString("C2")

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Alfred A.Commented:
Or, you can try the following

PAYMENT.Text = String.Format("{0:C2}",Convert.ToDecimal(Convert.ToDouble(AMOUNT.Text) * Convert.ToDecimal(RATE.Text)));
PAYMENT.Text = String.Format("{0:$#.##}",Convert.ToDecimal(Convert.ToDouble(AMOUNT.Text) * Convert.ToDecimal(RATE.Text)));
PAYMENT.Text = String.Format("{0:$#.00}",Convert.ToDecimal(Convert.ToDouble(AMOUNT.Text) * Convert.ToDecimal(RATE.Text)));
0
 
drypzCommented:
Hi! Like this

PAYMENT.Text = String.Format("{0:N2}", Convert.ToDecimal(AMOUNT.Text) * Convert.ToDecimal(RATE.Text))
0
 
sib_userAuthor Commented:
Thanks to all for so many choices.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now