How to format data using JavaScript

Source: Internet
Author: User
Tags xsl
How to use JavaScript Format data In the project, we have formatted and output data such as the amount number and date. For example, we have added "¥" before, and the value has two decimal places and a short date. Generally, high-level languages and development tools provide corresponding conversion functions, such as format. However, when I recently worked on a web-based online banking system, I encountered this very troublesome problem, javaScript does not provide this format function. I checked some information and found that VBScript has a Formatting Function. However, VBScript is dedicated to Microsoft, so I had to use a regular expression, regular Expressions cannot solve various types of conversions very commonly. It is really depressing to see an Ajax development tool demonstration, we can see that its data is formatted with the decimal point "¥" and "Number". We immediately downloaded the data and checked it. We found that it was a common Formatting Function using JavaScript + XSL, after downloading Ajax webshop, I find its system. open the JS file and find the following functions:
Function formatfloat (value, mask) {return basicformat (value, mask, 'formatnumber')} function formatdate (vardate, bstrformat, vardestlocale) {return basicformat (vardate, bstrformat, 'formatdate', vardestlocale);} function formattime (vartime, bstrformat, vardestlocale) {return basicformat (vartime, bstrformat, 'formattime', vardestlocale);} function basicformat (value, mask, action, Param) {var xmldoc; var callback Doc; vaR v = '<formats> <format> <value>' + value + '</value> <mask>' + mask + '</mask> </format> </ formats> '; xmldoc = parsexml (V); var X; If (isie) x = '<XSL: stylesheet xmlns: XSL = "URI: XSL"> 'else x =' <XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"> '; x + =' <XSL: template match = "/"> '; If (isie) {x + = '<XSL: eval>' + Action + '(' + value + ', "' + mask + '"'; If (PARAM) x + = ', '+ Param; x + =') </XSL: eval> ';} else x + =' <XSL: value-of select = "format-number ('+ value +',/'' + mask + '/') "/> '; x + =' </XSL: template> </XSL: stylesheet> '; Response Doc = parsexml (x); var s; If (isie) S = xmldoc. transformnode (Transport DOC) else {// for Mozilla/Netscape var processor = new transport tprocessor (); processor. importstylesheet (invalid DOC); var result = processor. transformtofragment (xmldoc, xmldoc); var xmls = new xmlserializer (); s = xmls. serializetostring (result);} return s ;}
It seems that their use of XSL is actually superb, so they can implement a very general format conversion. Of course you are welcome, and they are directly added to my function library. This code is not very understandable, but it does work very well. Please analyze it with friends who are proficient in XSL for us to learn.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.