Permalink
Browse files

original javascript-number-formatter

  • Loading branch information...
Mottie committed May 1, 2014
0 parents commit 8e488e28b787cfc8a90f6d0785617a2d924f2e64
Showing with 501 additions and 0 deletions.
  1. +22 −0 .gitattributes
  2. +148 −0 .gitignore
  3. +34 −0 README.md
  4. +75 −0 format.js
  5. +9 −0 format.min.js
  6. +213 −0 index.html
@@ -0,0 +1,22 @@
# Auto detect text files and perform LF normalization
* text=auto
# Custom for Visual Studio
*.cs diff=csharp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union
# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
@@ -0,0 +1,148 @@
#################
## Eclipse
#################
*.pydevproject
.project
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# PDT-specific
.buildpath
#################
## IntelliJ IDEA
#################
.idea/*
*.iml
#################
## Visual Studio
#################
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.sln.docstates
# Build results
[Dd]ebug/
[Rr]elease/
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.vspscc
.builds
*.dotCover
## TODO: If you have NuGet Package Restore enabled, uncomment this
#packages/
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
# Visual Studio profiler
*.psess
*.vsp
# ReSharper is a .NET coding add-in
_ReSharper*
# Installshield output folder
[Ee]xpress
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish
# Others
[Bb]in
[Oo]bj
sql
TestResults
*.Cache
ClientBin
stylecop.*
~$*
*.dbmdl
Generated_Code #added for RIA/Silverlight projects
# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
############
## Windows
############
# Windows image file caches
Thumbs.db
# Folder config file
Desktop.ini
# Installer logs
pip-log.txt
# Unit test / coverage reports
.coverage
.tox
#Translations
*.mo
#Mr Developer
.mr.developer.cfg
# Mac crap
.DS_Store
@@ -0,0 +1,34 @@
# Javascript Number Formatter
Lightweight & Fast JavaScript Number Formatter
## Introduction
This standalone number formatter is intended to be short and fast. As they are the main factors for a high performance JavaScript app. Development release is as short as 75 lines including license info, blank lines and comments. And production release is as small as 849 bytes.
```js
alert( format( "#,##0.####", 1234567.890)); //output: 1,234,567.89
```
## Features
* Short, fast, flexible yet standalone. Only 75 lines including MIT license info, blank lines & comments.
* Accept standard number formatting like `#,##0.00` or with negation `-000.####`.
* Accept any country format like `# ##0,00`, `#,###.##`, `#'###.##` or any type of non-numbering symbol.
* Accept any numbers of digit grouping. `#,##,#0.000` or `#,###0.##` are all valid.
* Accept any redundant/fool-proof formatting. `##,###,##.#` or `0#,#00#.###0#` are all OK.
* Auto number rounding.
* Simple interface, just supply mask & value like this: `format( "0.0000", 3.141592)`
## Limitations
* No prefix or suffix is allowed except leading negation symbol. So `$#,##0.00` or `#,###.##USD` will not yield expected outcome. Use `'$'+format('#,##0.00', 123.45)` or `format('#,##0.00', 456.789) + 'USD'`
* No scientific/engineering formatting.
* Not for date or phone formation.
* No color control.
## Note
When there's only one symbol is supplied, system will always treat the single symbol as Decimal. For instance, `format( '#,###', 1234567.890)` will output `1234567,890`. To force a single symbol as Separator, add a trailing dot to the end like this: `format( '#,###.', 1234567.890)` which will then output `1,234,567`.
A demo/sample page with few examples is provided ([DEMO]()). The code is safe to be minimized using Google Compiler in Advanced mode.
@@ -0,0 +1,75 @@
/**
* @preserve IntegraXor Web SCADA - JavaScript Number Formatter
* http://www.integraxor.com/
* author: KPL, KHL
* (c)2011 ecava
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
////////////////////////////////////////////////////////////////////////////////
// param: Mask & Value
////////////////////////////////////////////////////////////////////////////////
window['format'] = function( m, v){
if (!m || isNaN(+v)) {
return v; //return as it is.
}
//convert any string to number according to formation sign.
var v = m.charAt(0) == '-'? -v: +v;
var isNegative = v<0? v= -v: 0; //process only abs(), and turn on flag.
//search for separator for grp & decimal, anything not digit, not +/- sign, not #.
var result = m.match(/[^\d\-\+#]/g);
var Decimal = (result && result[result.length-1]) || '.'; //treat the right most symbol as decimal
var Group = (result && result[1] && result[0]) || ','; //treat the left most symbol as group separator
//split the decimal for the format string if any.
var m = m.split( Decimal);
//Fix the decimal first, toFixed will auto fill trailing zero.
v = v.toFixed( m[1] && m[1].length);
v = +(v) + ''; //convert number to string to trim off *all* trailing decimal zero(es)
//fill back any trailing zero according to format
var pos_trail_zero = m[1] && m[1].lastIndexOf('0'); //look for last zero in format
var part = v.split('.');
//integer will get !part[1]
if (!part[1] || part[1] && part[1].length <= pos_trail_zero) {
v = (+v).toFixed( pos_trail_zero+1);
}
var szSep = m[0].split( Group); //look for separator
m[0] = szSep.join(''); //join back without separator for counting the pos of any leading 0.
var pos_lead_zero = m[0] && m[0].indexOf('0');
if (pos_lead_zero > -1 ) {
while (part[0].length < (m[0].length - pos_lead_zero)) {
part[0] = '0' + part[0];
}
}
else if (+part[0] == 0){
part[0] = '';
}
v = v.split('.');
v[0] = part[0];
//process the first group separator from decimal (.) only, the rest ignore.
//get the length of the last slice of split result.
var pos_separator = ( szSep[1] && szSep[ szSep.length-1].length);
if (pos_separator) {
var integer = v[0];
var str = '';
var offset = integer.length % pos_separator;
for (var i=0, l=integer.length; i<l; i++) {
str += integer.charAt(i); //ie6 only support charAt for sz.
//-pos_separator so that won't trail separator on full length
if (!((i-offset+1)%pos_separator) && i<l-pos_separator ) {
str += Group;
}
}
v[0] = str;
}
v[1] = (m[1] && v[1])? Decimal+v[1] : "";
return (isNegative?'-':'') + v[0] + v[1]; //put back any negation and combine integer and fraction.
};
@@ -0,0 +1,9 @@
/*
IntegraXor Web SCADA - JavaScript Number Formatter
http://www.integraxor.com/
author: KPL, KHL
(c)2011 ecava
Dual licensed under the MIT or GPL Version 2 licenses.
*/
window.format=function(b,a){if(!b||isNaN(+a))return a;var a=b.charAt(0)=="-"?-a:+a,j=a<0?a=-a:0,e=b.match(/[^\d\-\+#]/g),h=e&&e[e.length-1]||".",e=e&&e[1]&&e[0]||",",b=b.split(h),a=a.toFixed(b[1]&&b[1].length),a=+a+"",d=b[1]&&b[1].lastIndexOf("0"),c=a.split(".");if(!c[1]||c[1]&&c[1].length<=d)a=(+a).toFixed(d+1);d=b[0].split(e);b[0]=d.join("");var f=b[0]&&b[0].indexOf("0");if(f>-1)for(;c[0].length<b[0].length-f;)c[0]="0"+c[0];else+c[0]==0&&(c[0]="");a=a.split(".");a[0]=c[0];if(c=d[1]&&d[d.length-
1].length){for(var d=a[0],f="",k=d.length%c,g=0,i=d.length;g<i;g++)f+=d.charAt(g),!((g-k+1)%c)&&g<i-c&&(f+=e);a[0]=f}a[1]=b[1]&&a[1]?h+a[1]:"";return(j?"-":"")+a[0]+a[1]};
Oops, something went wrong.

0 comments on commit 8e488e2

Please sign in to comment.