Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

JavaScript Number Parsing and Formatting for Multicultural Environments

, 21 Dec 2010 CPOL
A JavaScript class used to help in formatting and parsing numbers when parseInt and parseFloat are not enough
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

namespace NumberFormatterExample
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class FormattingServices
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        public void DoWork()
        {
            // Add your operation implementation here
            return;
        }

        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped)]
        public NumberFormatInfo GetFormat(string format)
        {
            if (format == "acc")
                return new NumberFormatInfo() { NegativeSign = "()", NumberDecimalDigits = 2, NumberDecimalSeparator = ".", NumberGroupSeparator = ",", NumberGroupSizes = 3, NumberNegativePattern = 0 };
            // Add your operation implementation here
            return new NumberFormatInfo(new System.Globalization.CultureInfo(format).NumberFormat);
        }

        // Add more operations here and mark them with [OperationContract]
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Luc Veronneau
Web Developer Faltour Inc.
Canada Canada
From the beginning i have always been passionate about everything. I have never actually studied in computer programming but life brought me in IT departments from the beginning of my career. The more i programmed the more i liked it. Ever since, i've been hooked.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 21 Dec 2010
Article Copyright 2010 by Luc Veronneau
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid