PDA

View Full Version : Decimals, keep some delete some


david.young
July 23rd, 2010, 01:42 PM
I know this should be easy but it's not happening.

When data has an amount in the decimal the client wants to keep it, $123.45
If it's just zero's they want to delete them, $123.00 would return as $123

Thanks
Dave
FP 5.8
Win XP sp 3
Acrobat 8

esmith
July 26th, 2010, 10:18 AM
var amount = Field("Value"); //your field
if (Right(amount, 2) == "00") return Left(amount, amount.length - 3);
else return amount;

david.young
July 29th, 2010, 02:44 PM
Eric,
I tried your rule doesn't seem to work.
I will try some variations and see what I can do
Thanks
Dave

Dan Korn
July 29th, 2010, 02:53 PM
return FormatNumber("$0.##", StringToNumber(Field("YourFieldName")));

david.young
July 29th, 2010, 03:33 PM
Thanks Dan,
Unfortunatley all it returns is $0
Was I supposed to do anything more than add my field name?
Thanks

Dan Korn
July 29th, 2010, 04:55 PM
Unfortunatley all it returns is $0
Was I supposed to do anything more than add my field name?
No, you should only have to replace the field name.

Exactly what is in the data? Does it already have a dollar sign in it? If so, you can do this instead:
return FormatNumber("$0.##", Trim(Field("YourFieldName")).replace(/^\$/,''));

david.young
July 29th, 2010, 06:05 PM
Hi Dan,
Yes the dollar sign is in the data file.
Your latest code returns the 2 zero's

Thanks
Dave

esmith
July 30th, 2010, 09:30 AM
If you still need help, I'm thinking a snippet of your data file would help us "see the big picture." ;)

Dan Korn
July 30th, 2010, 12:17 PM
Sorry, try this:
return Field("YourFieldName").replace(/\.0*$/,'');

david.young
August 2nd, 2010, 10:29 AM
Sorry Dan,
.00 still shows up
sample data file attached

Dan Korn
August 2nd, 2010, 11:14 AM
Sorry Dan,
.00 still shows up
sample data file attached
Okay, now I see. There's trailing white space in your data which is getting in the way. Try this:
return Trim(Field("LGAmount")).replace(/\.0*$/,'');

david.young
August 2nd, 2010, 12:45 PM
Dan
Thank You very much.
That did it.
What's with the trailing white space I've run into it before and it does'nt delete like a regular space?

Dave Young

Dan Korn
August 2nd, 2010, 03:54 PM
What's with the trailing white space I've run into it before and it does'nt delete like a regular space?
I'm not sure what you mean by "delete like a regular space." Sometimes white space is insignificant, such as when it's being interpreted by FusionPro's HTML-like tagged markup parser, but sometimes it is significant, such as in the regular expression in the code I posted.

Basically, this code:
return Field("LGAmount").replace(/\.0*$/,'');means "in the field value, if there's a period followed by zero or more 'zeros' at the end of the string, then replace all that with an empty string." When there's white space at the end of the field value, then that condition isn't true. So we need to use Trim to remove it first.

Another way to fix this would be to modify the regular expression to take the trailing white space into account:
return Field("LGAmount").replace(/\.0*\s*$/,'');This means, "in the field value, if there's a period followed by zero or more 'zeros' followed by zero or more white space characters at the end of the string, then replace all that with an empty string."

For reference on regular expressions:
https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions