quote:...Is there some way that I can display the following: $2.73M?...
TABLE FILE CAR PRINT CAR COMPUTE DOLLARS_INTEGER/I9C = 2733000; COMPUTE DOLLARS_ROUNDED/D12.2M = DOLLARS_INTEGER / 1000000; COMPUTE DOLLARS_DISPLAYED/A17 = FPRINT(DOLLARS_ROUNDED, 'D12.2M', 'A16') || 'M'; IF RECORDLIMIT EQ 1 ON TABLE SET STYLEMODE FIXED END
CAR DOLLARS_INTEGER DOLLARS_ROUNDED DOLLARS_DISPLAYED --- --------------- --------------- ----------------- JAGUAR 2,733,000 $2.73 $2.73M
quote:Originally posted by David Briars:quote:...Is there some way that I can display the following: $2.73M?...TABLE FILE CAR PRINT CAR COMPUTE DOLLARS_INTEGER/I9C = 2733000; COMPUTE DOLLARS_ROUNDED/D12.2M = DOLLARS_INTEGER / 1000000; COMPUTE DOLLARS_DISPLAYED/A17 = FPRINT(DOLLARS_ROUNDED, 'D12.2M', 'A16') || 'M'; IF RECORDLIMIT EQ 1 ON TABLE SET STYLEMODE FIXED ENDCAR DOLLARS_INTEGER DOLLARS_ROUNDED DOLLARS_DISPLAYED --- --------------- --------------- ----------------- JAGUAR 2,733,000 $2.73 $2.73M
DEFINE FILE ibisamp/car SALES_TIMES_1000/A20V=FTOA(CAR.BODY.SALES * 1000, '(D12.2)', SALES_TIMES_1000); -* The next line adds an asterisk in front of the sales number, to be used -* later in the JavaScript code below. JAVASCRIPT_MARKER/A50V='*' || SALES_TIMES_1000; END TABLE FILE ibisamp/car PRINT CAR.COMP.CAR AS 'Car' CAR.CARREC.MODEL AS 'Model' SALES_TIMES_1000 AS 'Sales' JAVASCRIPT_MARKER AS 'Human readable' BY CAR.COMP.CAR NOPRINT BY CAR.CARREC.MODEL NOPRINT ON TABLE HOLD AS OUTPUT FORMAT HTML ON TABLE NOTOTAL ON TABLE SET PAGE-NUM NOLEAD ON TABLE SET SQUEEZE ON ON TABLE SET EMPTYREPORT ON ON TABLE SET HTMLCSS ON ON TABLE SET HTMLENCODE ON ON TABLE SET CACHELINES 100 ON TABLE SET GRWIDTH 1 ON TABLE SET STYLE * ENDSTYLE END -RUN -HTMLFORM BEGIN <html> <head> <script language="javascript"> function replace() { // Examine all td tags and look for dollar amounts to convert // to a human-readable format. var x = document.getElementsByTagName("td"); var i; // If a td element begins with an asterisk, assume it is a dollar amount // that needs to be converted to a human-readable format. // // Remove asterisk and any commas first, and then convert to a // JavaScript floating point number before calling the "abbrNum" routine. for (i = 0; i < x.length; i++) { if (x[i].innerHTML.slice(1, 2) == "*") x[i].innerHTML = '$' + abbrNum(parseFloat(x[i].innerHTML.slice(2).replace(/,/g, '')), 2); } } // Function to make dollar amounts human-readable. Found on the Internet here: // http://stackoverflow.com/quest...ndly-format-e-g-1-1k function abbrNum(number, decPlaces) { // 2 decimal places => 100, 3 => 1000, etc decPlaces = Math.pow(10,decPlaces); // Enumerate number abbreviations var abbrev = [ "K", "M", "B", "T" ]; // Go through the array backwards, so we do the largest first for (var i=abbrev.length-1; i>=0; i--) { // Convert array index to "1000", "1000000", etc var size = Math.pow(10,(i+1)*3); // If the number is bigger or equal do the abbreviation if(size <= number) { // Here, we multiply by decPlaces, round, and then divide by decPlaces. // This gives us nice rounding to a particular decimal place. number = Math.round(number*decPlaces/size)/decPlaces; // Handle special case where we round up to the next abbreviation if((number == 1000) && (i < abbrev.length - 1)) { number = 1; i++; } // Add the letter for the abbreviation number += abbrev[i]; // We are done... stop break; } } return number; } </script> </head> <body onload="replace()"> !IBI.FIL.OUTPUT; </body> </html> -HTMLFORM END -EXIT
quote:Originally posted by David Briars:
@Squatch - Thanks for the link to the 'Reader Friendly' format Stack Overflow thread.
Taking a quick look at the algorithm provided in JavaScript there, it seems like the same kind of reformatting could be done in a WebFOCUS DEFINE FUNCTION.
DEFINE FUNCTION F_ABBRNUM(Number/F15.6, Decimals/I2) NumRound/P15 = Number; NumAbbr/A20 = 'K,M,B,T'; NumRLen/I11 = INT((ARGLEN(20, LJUST(20, FPRINT(NumRound, 'P20', 'A20'), 'A20'), 'I11') - 1) / 3); NumPrec/I11 = 10 ** (NumRLen * 3); NumOut/F15.6 = Number / NumPrec; NumSuff/A1 = GETTOK(NumAbbr, 20, NumRLen, ',', 1, 'A1'); Format/A8 = 'F15.'||LJUST(2,FPRINT(Decimals, 'I2', 'A2'), 'A2'); F_ABBRNUM/A20 = RJUST(20,FPRINT(NumOut, Format, 'A19')||NumSuff, 'A20'); END TABLE FILE GGSALES SUM COMPUTE NUMBER7/F15.6 = DOLLARS * 1000; COMPUTE DOLL_DISP/A20 = F_ABBRNUM(NUMBER7, 1); COMPUTE NUMBER6/F15.6 = DOLLARS * 100; COMPUTE DOLL_DISP/A20 = F_ABBRNUM(NUMBER6, 1); COMPUTE NUMBER5/F15.6 = DOLLARS * 10; COMPUTE DOLL_DISP/A20 = F_ABBRNUM(NUMBER5, 1); COMPUTE NUMBER4/F15.6 = DOLLARS; COMPUTE DOLL_DISP/A20 = F_ABBRNUM(NUMBER4, 1); COMPUTE NUMBER3/F15.6 = DOLLARS / 10; COMPUTE DOLL_DISP/A20 = F_ABBRNUM(NUMBER3, 1); COMPUTE NUMBER2/F15.6 = DOLLARS / 100; COMPUTE DOLL_DISP/A20 = F_ABBRNUM(NUMBER2, 1); COMPUTE NUMBER1/F15.6 = DOLLARS / 1000; COMPUTE DOLL_DISP/A20 = F_ABBRNUM(NUMBER1, 1); BY REGION END
In FOCUS since 1986 | WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2 | ||
WebFOCUS App Studio 8.2.06 standalone on Windows 10 |
quote:Originally posted by Tony A:
Try this, I know that it doesn't deal with the "pointy-haired boss" wants but it will give you an idea from which to start.
T
In FOCUS since 1986 | WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2 | ||
WebFOCUS App Studio 8.2.06 standalone on Windows 10 |
quote:I like these little tea-break posers
-* File HumanReadable.fex DEFINE FUNCTION F_ABBRNUM(Number/F15.6, Decimals/I2) Number/F19.2 = Number; Decimals/I2 = Decimals; NumRound/P15 = Number; NumAbbr/A20 = 'K,M,B,T'; NumRLen/I11 = INT((ARGLEN(20, LJUST(20, FPRINT(NumRound, 'P20', 'A20'), 'A20'), 'I11') - 1) / 3); NumPrec/P15 = INT(10 ** (NumRLen * 3)); NumOut/F19.2 = Number / NumPrec; -* -* Handle the special case where we round up to the next abbreviation. -* NumSpecial/P15 = NumOut; NumPrint/F19.2 = IF NumSpecial EQ 1000 AND NumRLen LT 4 THEN 1 ELSE NumOut; NumRLenAdj/I11 = IF NumSpecial EQ 1000 AND NumRLen LT 4 THEN NumRLen + 1 ELSE NumRLen; -* NumSuff/A1 = GETTOK(NumAbbr, 20, NumRLenAdj, ',', 1, 'A1'); Format/A8 = IF Decimals EQ 0 THEN 'F15' ELSE 'F19.'||LJUST(2,FPRINT(Decimals, 'I2', 'A2'), 'A2'); F_ABBRNUM/A21 = '$'||LJUST(20,FPRINT(NumPrint, Format, 'A19')||NumSuff, 'A20'); END -* TABLE FILE GGSALES PRINT SEQ_NO COMPUTE NEW_DOLLAR/P19.2C = DECODE SEQ_NO (1 12 2 0 3 1234 4 34567 5 918395 6 2134124 7 47475782130 8 999950 9 999950 10 999950000 11 10000000000000 12 12.50); AS 'Dollars' COMPUTE DECIMALS/I2 = DECODE SEQ_NO (1 1 2 2 3 0 4 2 5 1 6 2 7 2 8 0 9 1 10 0 11 0 12 1); AS 'Required,Decimals' COMPUTE DOLLAR_REFORMATED/A21 = F_ABBRNUM(NEW_DOLLAR, DECIMALS); AS 'Dollar,Display' -* IF SEQ_NO LE 12 -* ON TABLE SET STYLE * INCLUDE = endeflt, $ TYPE=TITLE, JUSTIFY = CENTER, $ TYPE=DATA, COLUMN = DOLLAR_REFORMATED, JUSTIFY = RIGHT, COLOR='WHITE',BACKCOLOR=RGB(#33a02c), STYLE = 'BOLD', $ ENDSTYLE END
quote:Originally posted by David Briars:
Awesome code share Tony!
I've added three items:
1. No decimal prints when 'decimals requested' is 0.
2. Handling of 'special case' where we round up to next abbreviation.
3. Output prefaced with '$'.
quote:The more I look at this, the more I think it should be a standard feature in IBI's software.
quote:Originally posted by David Briars:quote:The more I look at this, the more I think it should be a standard feature in IBI's software.
Yep, it would make a good format type.
I am guessing as folks are doing more and more 'KPI' type dashboard reports (v. columnar grid), this type of 'human readable' format might be more often requested.