Focal Point Banner


As of December 1, 2020, Focal Point is retired and repurposed as a reference repository. We value the wealth of knowledge that's been shared here over the years. You'll continue to have access to this treasure trove of knowledge, for search purposes only.

Join the TIBCO Community
TIBCO Community is a collaborative space for users to share knowledge and support one another in making the best use of TIBCO products and services. There are several TIBCO WebFOCUS resources in the community.

  • From the Home page, select Predict: WebFOCUS to view articles, questions, and trending articles.
  • Select Products from the top navigation bar, scroll, and then select the TIBCO WebFOCUS product page to view product overview, articles, and discussions.
  • Request access to the private WebFOCUS User Group (login required) to network with fellow members.

Former myibi community members should have received an email on 8/3/22 to activate their user accounts to join the community. Check your Spam folder for the email. Please get in touch with us at community@tibco.com for further assistance. Reference the community FAQ to learn more about the community.


Focal Point    Focal Point Forums  Hop To Forum Categories  WebFOCUS/FOCUS Forum on Focal Point     [SOLVED] Why is &AMTDIFF not formatting right like the rest of the values?:

Read-Only Read-Only Topic
Go
Search
Notify
Tools
[SOLVED] Why is &AMTDIFF not formatting right like the rest of the values?:
 Login/Join
 
Virtuoso
posted
Hey all!

So, I've been busy trying to replicate some of the functionality for the KPIs in the 8.1.03 Responsive Demo from IBI for our own app. The function that formats big numbers so that you see a K (thousands) or an M (millions), etc. instead of a huge number utilizes a regex to define what to match and replace with a comma from the string passed. I've tried to research the different parts of this regex to discover what it is searching for and don't quite get all of it. Could any of you help me figure out what it is searching for match-wise? I know it's looking for all matches of a string that doesn't start at the beginning/end that is followed by a sequence of 3 digits, but not followed by ??? <--That's where I get confused. Not followed by any digits? Doesn't make sense to me. Here's the code for the function:

function formatter(num,mode) {
   if (!isNaN(num)) {
      if (mode == 'K') {
         num = num / 1000;
         if (num < 1000)
            return num.toFixed(1).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 'K';
         else
            return num.toFixed(0).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 'K';
      }
      if (mode == 'M') {
         num = num / 1000000;
         if (num < 1000)
            return num.toFixed(1).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 'M';
         else
            return num.toFixed(0).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 'M';
      }
      if (mode == '%') {
         return num.trim() + '%';
      }
   }
   return num;
}


Or, if you know a better way to write a function that will shrink a number like 3,654,123 to show 3.6M instead, by all means share the love! lol

Thanks in advance for your help!

This message has been edited. Last edited by: CoolGuy,


8.2.02M (production), 8.2.02M (test), Windows 10, all outputs.
 
Posts: 1113 | Location: USA | Registered: January 27, 2015Report This Post
Expert
posted Hide Post
I'm not sure what the regex is trying to accomplish, but the toFixed method is new to me and interesting.

Here is a possible solution:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>fp_js1</title>

<script>
function reformatNumber(number, format) {
    switch (format) {
        case 'K':
            return( (number / 1000).toFixed(1) + format );
            break;

        case 'M':
            return( (number / 1000000).toFixed(1) + format );
            break;
    }
}

function testFormats() {
    alert( reformatNumber( 1234577776, 'K' ) );
    alert( reformatNumber( 1234577776, 'M' ) );
}

</script>

</head>

<body>

<button onclick="testFormats();">Test</button>

</body>
</html>


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
 
Posts: 10577 | Location: Toronto, Ontario, Canada | Registered: April 27, 2005Report This Post
Virtuoso
posted Hide Post
Francis,

Thanks for your suggestion. .toFixed() is supposedly a new method in ES5 that lets you take a number and set the precision of the trailing decimals for the number. Pretty slick. I found that the regex is a pretty intense but slick way to insert commas where they should go by:

- Using 2 look ahead type assertions:
- 1 positive assertion that looks for any point in the string that has a
multiple of 3 digits in a row after it;
- 1 negative assertion that makes sure that point only has exactly a
multiple of 3 digits. The replacement expression puts a comma there.

Ex.: If you pass "123456789.01", the positive assertion will match every spot to the left of the 7 (since "789" is a multiple of 3 digits, "567", etc.). The negative assertion checks that the multiple of 3 digits does not have any digits after it. "789" has a period after it so it is exactly a multiple of 3 digits, so a comma goes there. "678" is a multiple of 3 digits but it has a "9" after it, so those 3 digits are part of a group of 4, and a comma does not go there. Similarly, for "567". "456789" is 6 digits, which is a multiple of 3, so a comma goes before that. "345678" is a multiple of 3, but it has a "9" after it, so no comma goes there. And so on. The "\B" keeps the regex from putting a comma at the beginning of the string.

I created a function that should work for dynamically passed values where you do not know how large the number is yet (so "mode" doesn't work). It works with one gadget I've created from the Responsive Demo KPI gadgets, but not with another I've applied the same function to in the same way. Code is exactly the same. I am baffled as to why it doesn't work. Do you or anyone here see anything wrong with my code?:

-HTMLFORM BEGIN
<html>
<head>
<link rel='stylesheet' id='options_typography_Montserrat-regular-css'  href='https://fonts.googleapis.com/css?family=Montserrat:regular' type='text/css' media='all' />
<link rel='stylesheet' id='options_typography_Montserrat-400-css'  href='https://fonts.googleapis.com/css?family=Montserrat:400' type='text/css' media='all' />
<link rel="stylesheet" type="text/css" href="/ibi_apps/jquery/css/smoothness/jquery-ui.custom.min.css">
<script type="text/javascript" src="/ibi_apps/jquery/js/jquery.min.js"></script>
<script type="text/javascript" src="/ibi_apps/jquery/js/jquery-ui.custom.min.js"></script>
<link rel='stylesheet' href='/ibi_apps/run.bip?BIP_REQUEST_TYPE=BIP_RUN&|BIP_folder=IBFS%253A%252FWFC%252FRepository%252Fretail_reporting%252FHidden_Content%252Fstyles%252F&|BIP_item=gadget.css' type='text/css' media='all' />
<script>
function formatNum(num) {
	var suffixK = "K";
	var suffixM = "M";
	if (num > 999 && num < 1000000) {
		num = num / 1000;
		num = num.toFixed(1).concat(suffixK);
		return num;
	}
	else if (num > 999999){
		num = num / 1000000;
		num = num.toFixed(1).concat(suffixM);
		return num;
	}
	else {
		return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	}
}

function loadNumbers() {
  $('#num1').html(formatNum('!IBI.AMP.ACT_VAL;'));
  $('#num2').html(formatNum('!IBI.AMP.COMP_VAL1;'));
  $('#num3').html(formatNum('!IBI.AMP.COMP_VAL2;'));
  $('#num4').html(formatNum('!IBI.AMP.COMP_VAL3;'));
}
</script>
</head>
<body onLoad="loadNumbers()">
<div class="!IBI.AMP.CLASS;" style="border-radius: 0px; padding-top: 5px; padding-bottom: 5px">
<div style="width:100%">
<span style="font-size:1em">!IBI.AMP.KPI_NAME;</span><br>
<span style="font-size:.5em">!IBI.AMP.HEAD;</span><br>
<span id="num1" style="font-size:2em"></span>
</div>
<div style="width:33%;float:left;padding:0;margin:0">
<span style="font-size:.6em">!IBI.AMP.COMP_LBL1;</span><br><span id="num2"></span>
</div>
<div style="width:33%;float:left;padding:0;margin:0">
<span style="font-size:.6em">!IBI.AMP.COMP_LBL2;</span><br><span id="num3"></span>
</div>
<div style="width:33%;float:left;padding:0;margin:0">
<span style="font-size:.6em">!IBI.AMP.COMP_LBL3;</span><br><span id="num4"></span>
</div>
</div>
</body>
</html>
-HTMLFORM END


8.2.02M (production), 8.2.02M (test), Windows 10, all outputs.
 
Posts: 1113 | Location: USA | Registered: January 27, 2015Report This Post
Virtuoso
posted Hide Post
After messing around with the code, I found that having the '$' sign in front of the & variable in the DM commands that set the values made my Js function formatNum() not work. After taking out the '$' sign in front, the formatting applied to those values finally (except for &AMTDIFF for some unknown reason).

I should of given you all the code. Sorry. If you or anyone knows why &AMTDIFF wouldn't be getting the formatting like the others, let me know. Appreciate your help. Here's the rest of it:

-* Updated Guests % Variance KPI controller.
-* Date: 5/11/2015

-* Enable all default amper variables
-INCLUDE IBFS:/WFC/Repository/retail_reporting/retail_reports/prod_include_ibimruser_parms.fex

-* Clean up work files
APP DELETEF RETAIL_REPORTS HOLD MAS
APP DELETEF RETAIL_REPORTS HOLD FOC
APP DELETEF RETAIL_REPORTS HOLD FTM
-RUN

-SET &DSDIVISION = IF &DSDIVISION EQ 'ALL' OR '_FOC_NULL' THEN 'FOC_NONE' ELSE &DSDIVISION;
-SET &DSDISTRICT = IF &DSDISTRICT EQ 'ALL' OR '_FOC_NULL' THEN 'FOC_NONE' ELSE &DSDISTRICT;
-SET &DSBANNERGL = IF &DSBANNERGL EQ 'ALL' OR '_FOC_NULL' THEN 'FOC_NONE' ELSE &DSBANNERGL;
-SET &DSACCTNO   = IF &DSACCTNO   EQ 'ALL' OR '_FOC_NULL' THEN 'FOC_NONE' ELSE &DSACCTNO;
-SET &DDDEPTCODE = IF &DDDEPTCODE EQ 'ALL' OR '_FOC_NULL' THEN 'FOC_NONE' ELSE &DDDEPTCODE;

-GOTO &PERIOD;

-PD
-SET &VAR1='FISCALWEEKOFYEAR EQ &FISCALWEEKOFYEAR.EVAL';
-SET &VAR2='WEEKDAY EQ &WEEKDAY.EVAL';
-GOTO REQUEST;

-WTD
-SET &VAR1='FISCALWEEKOFYEAR EQ &FISCALWEEKOFYEAR.EVAL';
-SET &VAR2='FOC_NONE';
-GOTO REQUEST;

-QTD
-SET &VAR1='FISCALQUARTER EQ &FISCALQUARTER.EVAL';
-SET &VAR2='FOC_NONE';
-GOTO REQUEST;

-REQUEST
TABLE FILE AFSTEMP
SUM
CCUSTTOTAL PCUSTTOTAL
COMPUTE PCTDIFF/D9.2=((CCUSTTOTAL-PCUSTTOTAL)/PCUSTTOTAL)*100;
COMPUTE AMTDIFF/D15  =(CCUSTTOTAL-PCUSTTOTAL);
WHERE (FISCALYEAR EQ '&FISCALYEAR');
WHERE (&VAR1);
WHERE (&VAR2);
WHERE DSDIVISION EQ &DSDIVISION;
WHERE DSDISTRICT EQ &DSDISTRICT;
WHERE DSBANNERGL EQ &DSBANNERGL;
WHERE DSACCTNO   EQ &DSACCTNO;
WHERE DDDEPTCODE EQ &DDDEPTCODE;
WHERE (CSALES NE 0);
ON TABLE SAVE
END
-RUN
-READ SAVE &CCUSTTOTAL.A11. &PCUSTTOTAL.A11. &PCTDIFF.A9. &AMTDIFF.A15.

-SET &CCUSTTOTAL = (&CCUSTTOTAL + 0);
-SET &PCUSTTOTAL = (&PCUSTTOTAL + 0);
-SET &PCTDIFF = (&PCTDIFF + 0);
-SET &AMTDIFF = (&AMTDIFF + 0);

-SET &DATEFULL = EDIT(&DATEFULL, '9/99/9999');

-SET &KPI_NAME = IF &PERIOD EQ 'PD' THEN 'PD Guests % Variance' ELSE
-IF &PERIOD EQ 'WTD' THEN 'WTD Guests % Variance' ELSE
-IF &PERIOD EQ 'QTD' THEN 'QTD Guests % Variance';

-SET &HEAD = IF &PERIOD EQ 'PD' THEN 'Fiscal Year: &FISCALYEAR.EVAL - Fiscal Week: &FISCALWEEKOFYEAR.EVAL - Date: &DATEFULL.EVAL' ELSE
-IF &PERIOD EQ 'WTD' THEN 'Fiscal Year: &FISCALYEAR.EVAL - Fiscal Week: &FISCALWEEKOFYEAR.EVAL - Date: &DATEFULL.EVAL' ELSE
-IF &PERIOD EQ 'QTD' THEN 'Fiscal Year: &FISCALYEAR.EVAL - Fiscal Quarter: &FISCALQUARTER.EVAL - Date: &DATEFULL.EVAL';

-SET &COMP_LBL1 = IF &PERIOD EQ 'PD' THEN 'This Day LY' ELSE
-IF &PERIOD EQ 'WTD' THEN 'This Week LY' ELSE
-IF &PERIOD EQ 'QTD' THEN 'This Quarter LY';

-DEFAULTH &COMP_LBL2 = '% Variance';
-DEFAULTH &COMP_LBL3 = 'Amt Variance';
-DEFAULTH &ACT_VAL = '&CCUSTTOTAL.EVAL';
-DEFAULTH &COMP_VAL1 = '&PCUSTTOTAL.EVAL';

-SET &COMP_VAL2 = IF (&PCTDIFF GT 0) THEN '&PCTDIFF.EVAL% ' | '&|#x25B2;' ELSE
-IF (&PCTDIFF LT 0) THEN '&PCTDIFF.EVAL% ' | '&|#x25BC;' ELSE '&PCTDIFF.EVAL% ' | '&|#x25c6;';

-DEFAULTH &COMP_VAL3 = '&AMTDIFF.EVAL';

-SET &CLASS = IF (&PCTDIFF GT 0) THEN 'green-alert' ELSE
-IF (&PCTDIFF LT 0) THEN 'red-alert' ELSE 'orange-alert';

-HTMLFORM BEGIN
<html>
<head>
<link rel='stylesheet' id='options_typography_Montserrat-regular-css'  href='https://fonts.googleapis.com/css?family=Montserrat:regular' type='text/css' media='all' />
<link rel='stylesheet' id='options_typography_Montserrat-400-css'  href='https://fonts.googleapis.com/css?family=Montserrat:400' type='text/css' media='all' />
<link rel="stylesheet" type="text/css" href="/ibi_apps/jquery/css/smoothness/jquery-ui.custom.min.css">
<script type="text/javascript" src="/ibi_apps/jquery/js/jquery.min.js"></script>
<script type="text/javascript" src="/ibi_apps/jquery/js/jquery-ui.custom.min.js"></script>
<link rel='stylesheet' href='/ibi_apps/run.bip?BIP_REQUEST_TYPE=BIP_RUN&|BIP_folder=IBFS%253A%252FWFC%252FRepository%252Fretail_reporting%252FHidden_Content%252Fstyles%252F&|BIP_item=gadget.css' type='text/css' media='all' />
<script>
function formatNum(num) {
	var suffixK = "K";
	var suffixM = "M";
	if (num > 999 && num < 1000000) {
		num = num / 1000;
		num = num.toFixed(1).concat(suffixK);
		return num;
	}
	else if (num > 999999){
		num = num / 1000000;
		num = num.toFixed(1).concat(suffixM);
		return num;
	}
	else {
		return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	}
}

function loadNumbers() {
  $('#num1').html(formatNum('!IBI.AMP.ACT_VAL;'));
  $('#num2').html(formatNum('!IBI.AMP.COMP_VAL1;'));
  $('#num3').html(formatNum('!IBI.AMP.COMP_VAL2;'));
  $('#num4').html(formatNum('!IBI.AMP.COMP_VAL3;'));
}
</script>
</head>
<body onLoad="loadNumbers()">
<div class="!IBI.AMP.CLASS;" style="border-radius: 0px; padding-top: 5px; padding-bottom: 5px">
<div style="width:100%">
<span style="font-size:1em">!IBI.AMP.KPI_NAME;</span><br>
<span style="font-size:.5em">!IBI.AMP.HEAD;</span><br>
<span id="num1" style="font-size:2em"></span>
</div>
<div style="width:33%;float:left;padding:0;margin:0">
<span style="font-size:.6em">!IBI.AMP.COMP_LBL1;</span><br><span id="num2"></span>
</div>
<div style="width:33%;float:left;padding:0;margin:0">
<span style="font-size:.6em">!IBI.AMP.COMP_LBL2;</span><br><span id="num3"></span>
</div>
<div style="width:33%;float:left;padding:0;margin:0">
<span style="font-size:.6em">!IBI.AMP.COMP_LBL3;</span><br><span id="num4"></span>
</div>
</div>
</body>
</html>
-HTMLFORM END


8.2.02M (production), 8.2.02M (test), Windows 10, all outputs.
 
Posts: 1113 | Location: USA | Registered: January 27, 2015Report This Post
Virtuoso
posted Hide Post
Will come back to this issue another time. Thanks for those of you who helped as much as you did.


8.2.02M (production), 8.2.02M (test), Windows 10, all outputs.
 
Posts: 1113 | Location: USA | Registered: January 27, 2015Report This Post
Virtuoso
posted Hide Post
A couple of things caught my attention, although they may not fix your problem.
First, why specify alpha reads when the data is numeric?

-READ SAVE &CCUSTTOTAL.11. &PCUSTTOTAL.11. &PCTDIFF.9. &AMTDIFF.15.

This should eliminate the need for the next step of adding zeroes to the values (although I'm not sure it was ever necessary in the first place).

-DEFAULT commands are not terminated with a semi-colon, so I would remove those.

And again, why make numeric values alpha by putting quotes around them?

-DEFAULTH &ACT_VAL   = &CCUSTTOTAL.EVAL
-DEFAULTH &COMP_VAL1 = &PCUSTTOTAL.EVAL

-DEFAULTH &COMP_VAL3 = &AMTDIFF.EVAL

I assume you checked the final values in the DM variables with -TYPEs before the -HTMLFORM to ensure they are correct? If not, I would definitely do that.

Finally, do all of the values except &AMTDIFF have decimals? If so, I would try adding a decimal to &AMTDIFF to see if it makes a difference.

COMPUTE AMTDIFF/D15.1 =(CCUSTTOTAL-PCUSTTOTAL);

This message has been edited. Last edited by: Dan Satchell,


WebFOCUS 7.7.05
 
Posts: 1213 | Location: Seattle, Washington - USA | Registered: October 22, 2007Report This Post
Virtuoso
posted Hide Post
Dan,

Thanks for looking over my code and trying to help. You would think the data would be numeric, but it is alpha within the tables it's mapped from. Goofy huh? (Note: I didn't create the data source tables, data descriptions, etc. I am merely stuck using them for now.) Thanks for the insight on no semi-colons with -DEFAULT/DEFAULTH commands. I use them because it makes me feel better. I have tried to add the decimal precision to &AMTDIFF and the others, but then no values show up on the gadget. Thanks for trying though.


8.2.02M (production), 8.2.02M (test), Windows 10, all outputs.
 
Posts: 1113 | Location: USA | Registered: January 27, 2015Report This Post
Virtuoso
posted Hide Post
Hey all,

I actually found the problem to be up where my request was. I needed to include &AMTDIFF as a COMPUTE field and not a -SET/-DEFAULTH like I had done. Goofy me.


8.2.02M (production), 8.2.02M (test), Windows 10, all outputs.
 
Posts: 1113 | Location: USA | Registered: January 27, 2015Report This Post
  Powered by Social Strata  

Read-Only Read-Only Topic

Focal Point    Focal Point Forums  Hop To Forum Categories  WebFOCUS/FOCUS Forum on Focal Point     [SOLVED] Why is &AMTDIFF not formatting right like the rest of the values?:

Copyright © 1996-2020 Information Builders