Using asp how to create formatted currency with co

2019-08-11 02:17发布

问题:

Using asp. Trying to format a decimal number to add commas. Are there simple to use functions or techniques in asp to go from a decimal value to currency format with commas?

Examples:

DecimalValue = 3439.01     CurrencyValue = "    3,439.01"
DecimalValue = 3843838.38  CurrencyValue = "3,843,838.00"

回答1:

use the vbscript function FormatCurrency

complete syntax is: FormatCurrency(Expression[,NumDigAfterDec[, IncLeadingDig[,UseParForNegNum[,GroupDig]]]])

example:

FormatCurrency(20000)

output = $20,000.00

example of setting number of decimals:

FormatCurrency(20000,5)

output = $20,000.00000



回答2:

To expand on Carlton Jenke's answer, there are 2 functions you can use for this (you mentioned formatting as a currency in the question title but don't include currency symbols in the body of the question):

  1. formatnumber returns an expression formatted as a number.
  2. formatcurrency returns an expression formatted as a currency value using the currency symbol defined in the system control panel.

Both functions take the same arguments, those being:

Expression [,NumDigitsAfterDecimal [,IncludeLeadingDigit
[,UseParensForNegativeNumbers [,GroupDigits]]]]
  1. Expression is the only required argument, that being the number you wish to format.
  2. NumDigitsAfterDecimal is a numeric value specifying how many decimal places you want to round to. The default value is -1, which indicates that the computer's regional settings should be used.
  3. IncludeLeadingDigit is a tristate constant (see below) which specifies whether or not you want to include a leading zero for values between -1 and 1.
  4. UseParensForNegativeNumbers is another tristate constant which specifies whether or not you want negative values to be enclosed in parentheses, rather than using a minus symbol.
  5. GroupDigits, which is the argument you're after, is also a tristate constant and is used to specify whether or not you want to group numbers using the system's group delimiter.

The tristate constants takes one of the following for the value:

  1. -2 is the default and indicates that the default value from the computer's regional setting should be used.
  2. -1 is true.
  3. 0 is false.