5.3 printShowingDecimalPlaces: 2 displays '5.29' insteadof'5.30'

nicolas cellier ncellier at ifrance.com
Thu Nov 22 21:03:58 UTC 2007


Sebastian Sastre a écrit :
> Ok, don't beat me.. I'm not proud of this but I have to use until I 
> manage to have something better.
>  
> String>>asScaledDecimal: aQuantityOfDigits
>  "Answers the scaled decimal with aQuantityOfDigits
>  of precision represented by the receiver."
>  
>  ^ [:number|
>   number isZero
>    ifTrue:[0 asFloat asScaledDecimal: aQuantityOfDigits]
>    ifFalse:[number asScaledDecimal: aQuantityOfDigits]] value: self 
> asNumber 
>  

Yes, it should be (ScaledDecimal readFrom: self), but this one would 
anwer a Float...

> so my converter can do this depending on it's own configuration (not 
> only used for money):
>  
> MVPNumberToText>>rightToLeft: aString
>  "Answers the result of converting aString to a Number or aScaledDecimal."
>  
>  ^self hasInputDigits
>   ifTrue:[aString asScaledDecimal: inputDigits]
>   ifFalse:[aString asNumber]
>  
> Adding the horrible hack:
>  
> ScaledDecimal>>printOn: aStream
>  "Reimplementation - Object 'printing' method."
>  | aFraction tmpFractionPart |
>  self < 0 ifTrue: [aStream nextPut: $-].
>  aFraction := fraction abs.
>  aStream nextPutAll: aFraction rounded printString.
>  scale = 0 ifTrue: [^ aStream nextPutAll: 's0'].
>  aStream nextPut: $..
>  tmpFractionPart := aFraction fractionPart.
>  1 to: scale
>   do:
>    [:dummy |
>    tmpFractionPart := tmpFractionPart * 10.
>    (10 <= tmpFractionPart rounded)
>     ifTrue:[aStream nextPut: $0]
>     ifFalse:[aStream nextPut: (Character digitValue: tmpFractionPart 
> rounded)].
>    tmpFractionPart := tmpFractionPart fractionPart].
>  
> I was able to make the values to behave,
>  
>     thanks,
>  
> 
> Sebastian Sastre
> 
>  
> 
> 
>     ------------------------------------------------------------------------
>     *De:* squeak-dev-bounces at lists.squeakfoundation.org
>     [mailto:squeak-dev-bounces at lists.squeakfoundation.org] *En nombre de
>     *tim Rowledge
>     *Enviado el:* Jueves, 22 de Noviembre de 2007 17:10
>     *Para:* The general-purpose Squeak developers list
>     *Asunto:* Re: 5.3 printShowingDecimalPlaces: 2 displays '5.29'
>     insteadof'5.30'
> 
> 
>     On 22-Nov-07, at 11:00 AM, Sebastian Sastre wrote:
> 
>>     Thanks Tim,
>>        
>>         may be I should clarify that it's only for presentation. The
>>     mone value is stored as anMeasure of it's monetary unit in the
>>     Aconcagua framework.
>>      
>>         But I'm curious about your answer. Let's supose the user
>>     enters in some input widget with keyboard the '5.30' value. That
>>     will end as a Float at some point. Do you suggest I convert that
>>     to a ScaledDecimal? (to store/comare, etc)?
>>      
>>         I'm evaluating this:
>>      
>>      (5.3 asScaledDecimal: 2)
>>         and also obtainig:  ' 5.29s2'
> 
>     That must surely count as a bug and I'd hazard a guess that it
>     happens because some code is comparing floats; I've not looked at
>     the code but I've seen it too many times not to suspect it.
> 
>     As for your input widget, I'd try to make it give you the string and
>     directly convert that to a 2dp scaled decimal rather than letting it
>     give you a float. If there isn't a String>asScaledFloat: type method
>     then I guess you get to write it :-)
> 
> 
>     tim
>     --
>     tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
>     All the simple programs have been written, and all the good names taken.
> 
> 
> 
> ------------------------------------------------------------------------
> 
> 




More information about the Squeak-dev mailing list