Share via


Code to Convert Between Numbers and Strings

The following two examples illustrate the use of the DataFunctions object in converting between string values and numbers.

Converting a Number to a String

Converting a Monetary Value to a Number

Converting a Number to a String

In this example, you create an Active Server Pages (ASP) page that converts a floating-point number into a variety of outputs. All outputs are based on the US locale; this example explores different rounding and trailing zero variable settings.

Create the example using the following code:

  1. Instantiate the DataFunctions object, and set the object Locale property to US.

    Dim oDataFunctions, iResult, iLocale, iInputFloat
    Set oDataFunctions = Server.CreateObject("Commerce.DataFunctions")
    iLocale = "1033"
    oDataFunctions.Locale = iLocale
    
  2. Call the Float method to convert the number to the US version with no rounding and with trailing zeros maintained. Write the results to the page.

    iInputFloat = 100.00010
    iResult = oDataFunctions.Float(iInputFloat, iLocale, True , False)
    Response.Write "USA, No Rounding, With Trailing: " & Result & "<P>"
    
  3. Call the Float method to convert the number to the US version with no rounding and with trailing zeros removed. Write the results to the page.

    Result = oDataFunctions.Float(iInputFloat, iLocale, True, True)
    Response.Write "USA, No Rounding, Trim Trailing: " & Result & "<P>"
    
  4. Call the Float method to convert the number to the US version with rounding and with trailing zeros maintained. Write the results to the page.

    Result = oDataFunctions.Float(iInputFloat, iLocale, False, False)
    Response.Write "USA, Rounding, With Trailing: " & Result & "<P>"
    
  5. Call the Float method to convert the number to the US version with rounding and with trailing zeros removed. Write the results to the page.

    Result = oDataFunctions.Float(iInputFloat, iLocale, False, True)
    Response.Write "USA, Rounding, Trim Trailing: " & Result & "<P>"
    

Converting a Monetary Value to a Number

In this example, you create an ASP page that converts a monetary value to an integer. The ConvertMoneyStringToNumber method returns the value in the base monetary unit for a given Locale property.

Thus, given a value in US dollars, the ConvertMoneyStringToNumber method returns the number of cents in the US dollar value. Additionally, the ConvertMoneyStringToNumber method does not round values. For example, if you pass to this method the number "123.009", the method returns 12300, not 12301.

Create the example using the following code:

  1. Instantiate the DataFunctions object.

    Dim oDataFunctions, iResult, sAmount, iLocale
    sAmount = "12000"
    Set oDataFunctions = Server.CreateObject("Commerce.DataFunctions")
    
  2. Set locale to US.

    iLocale = "1033" 
    oDataFunctions.Locale = iLocale
    
  3. Convert the string to an integer.

    Result = oDataFunctions.ConvertMoneyStringToNumber("34.095", iLocale)
    
  4. Write the results to the page.

    Response.Write "USA: " & Result & "<P>"
    

Copyright © 2005 Microsoft Corporation.
All rights reserved.