[ic] [PATCH] Expand range of round_to_frac_digits to handle exponentized numbers

Brian J. Miller wiggins at danconia.org
Wed Sep 2 16:23:25 UTC 2009


Mike Heins wrote:
> Quoting Brian J. Miller (brian at endpoint.com):
>> ---
>>  lib/Vend/Util.pm |   20 +++++++++++++++-----
>>  1 files changed, 15 insertions(+), 5 deletions(-)
>>
>> diff --git a/lib/Vend/Util.pm b/lib/Vend/Util.pm
>> index 8356b84..5be6b5a 100644
>> --- a/lib/Vend/Util.pm
>> +++ b/lib/Vend/Util.pm
>> @@ -269,12 +269,22 @@ sub round_to_frac_digits {
>>  	else {
>>  		$digits = 2;
>>  	}
>> -	my @frac;
>> -	$num =~ /^(-?)(\d*)(?:\.(\d+))?$/
>> -		or return $num;
>> +	if ($num =~ /^(-?)(\d*)(?:\.(\d+)(?:e-(\d+)))?$/) {
>> +		# this number sufficiently close to zero for our purposes
>> +		return 0;
>> +	}
> 
> Don't we want to return 0.00 (or whatever the frac_digits are? What if it is -1?
> 

I guess it depends on the context used (aka numeric vs. string), but yes 
I'd be fine with that. Do you mean if $frac_digits is -1? I'm not clear 
exactly the use case for the subroutine, hence my e-mail here. Carl 
Bailey also suggested that if $frac_digits is 0 there is currently a 
trailing "." included which might need to be removed.

If you can provide more clarity above I'm happy to whip something up. 
(I'm on early vacation for the US Labor Day starting tomorrow, so it 
will probably be next week before I have something else out, but I 
*will* follow up if no one beats me to it.)

>> +	elsif ($num =~ /^(-?)(\d*)(?:\.(\d+))?$/) {
>> +		# no op
>> +	}
>> +	else {
>> +		warn "Vend::Util::round_to_frac_digits: invalid number ($num)\n";
>> +		return $num;
>> +	}
>> +
>>  	my $sign = $1 || '';
>> -	my $int = $2;
>> -	@frac = split(m{}, ($3 || 0));
>> +	my $int  = $2;
>> +	my @frac = split(m{}, ($3 || 0));
>> +
>>  	local($^W) = 0;
>>  	my $frac = join "", @frac[0 .. $digits - 1];
>>  	if($frac[$digits] > 4) {
>> -- 
>> 1.5.6.3
>>
>>
>> _______________________________________________
>> interchange-users mailing list
>> interchange-users at icdevgroup.org
>> http://www.icdevgroup.org/mailman/listinfo/interchange-users
>>
> 


-- 
Brian J. Miller
End Point Corp.
http://www.endpoint.com/
brian at endpoint.com



More information about the interchange-users mailing list