Hi all,
I have Float in form: 12.0 123.3453 etc and want to format it to two-digits-after-dot. Searched through classes but found nothing suitable except n := 12.6. String streamContents: [:s| n absPrintOn: s base: 10 digitCount: n asInteger asString size + 2. ] while it works fine for any float like 12.3245 it does not add zero to float like 12.6 :-( I would like to see 12.60 Is there simple way to do it ? like fprintf in C/Perl world ? Thanks. -Dmitry. _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
> I would like to see 12.60 > > Is there simple way to do it ? > like fprintf in C/Perl world ? > Try this. (amount * 100) asInteger printStringAsCents. _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
Michel Bany wrote:
> >> I would like to see 12.60 >> >> Is there simple way to do it ? >> like fprintf in C/Perl world ? >> > Try this. > (amount * 100) asInteger printStringAsCents. Also, you might consider using an integer number of cents or even a Money class to represent money, rather than a float. Floating point arithmetic doesn't produce the kind of results you'd expect for money... if you know this already, just ignore me. Colin _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Dmitry Dorofeev
Try this ;-) [ :float :digit | | s | float = 0 ifTrue: [ s := (String new: digit+1) atAllPut: $0 ] ifFalse: [ s := (float*(10**digit)) asInteger printString ]. (s copyFrom: 1 to: (s size - digit)) , '.' , (s copyFrom: (s size -digit+1) to: s size) ] value: 0.0 value: 2 Dmitry Dorofeev wrote: > Hi all, > > I have Float in form: > 12.0 > 123.3453 > etc > and want to format it to two-digits-after-dot. > Searched through classes but found nothing suitable except > > n := 12.6. > String streamContents: [:s| > n absPrintOn: s base: 10 digitCount: n asInteger asString size + 2. > ] > > while it works fine for any float like 12.3245 > it does not add zero to float like 12.6 :-( > I would like to see 12.60 > > Is there simple way to do it ? > like fprintf in C/Perl world ? > > Thanks. > -Dmitry. > _______________________________________________ > Seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside > -- ------------------------- Luc Damas Assistant professor Condillac, LISTIC, ESIA University of Savoie France http://ontology.univ-savoie.fr/luc.damas _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Dmitry Dorofeev
Dmitry Dorofeev wrote:
> Hi all, > > I have Float in form: > 12.0 > 123.3453 > etc > and want to format it to two-digits-after-dot. > Searched through classes but found nothing suitable except In a 3.8 derived image, I get: 12.0 printShowingDecimalPlaces: 2. ==> '12.00' 123.3453 printShowingDecimalPlaces: 2. ==> '123.35' _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
What about the compatibility between Squeak and VW? > In a 3.8 derived image, I get: > > > 12.0 printShowingDecimalPlaces: 2. ==> '12.00' > 123.3453 printShowingDecimalPlaces: 2. ==> '123.35' > _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Michel Bany-3
Michel Bany wrote:
> >> I would like to see 12.60 >> >> Is there simple way to do it ? >> like fprintf in C/Perl world ? >> > Try this. > (amount * 100) asInteger printStringAsCents. > If 'amount' is, say, 12.499999, I think 12.50 is the desirable answer. The above will instead yield 12.49. Nevin _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Luc Damas
Luc Damas wrote:
> > What about the compatibility between Squeak and VW? > >> In a 3.8 derived image, I get: > >> 12.0 printShowingDecimalPlaces: 2. ==> '12.00' >> 123.3453 printShowingDecimalPlaces: 2. ==> '123.35' >> A compatibility package - you're going to need one eventually if you need to run in both environments. If Squeak is your mainline then you'll need a SqueakCompatibility in VW, and vice versa. _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Luc Damas
Luc Damas a écrit :
> > What about the compatibility between Squeak and VW? > > With Seaside loaded, this should work in both : (amount * 100) rounded printStringAsCents. _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Dmitry Dorofeev
I had the same need, so I added this message in Float :
---------- roundedTo: precision "Answer the float truncated at precision decimal" (self rounded = self) ifTrue: [ ^ self rounded ] ifFalse: [ ^ (((self * (10 raisedTo: precision)) rounded) / (10 raisedTo: precision)) asFloat ] ---------- It's working fine for me. Florian Dmitry Dorofeev wrote: > Hi all, > > I have Float in form: > 12.0 > 123.3453 > etc > and want to format it to two-digits-after-dot. > Searched through classes but found nothing suitable except > > n := 12.6. > String streamContents: [:s| > n absPrintOn: s base: 10 digitCount: n asInteger asString size + 2. > ] > > while it works fine for any float like 12.3245 > it does not add zero to float like 12.6 :-( > I would like to see 12.60 > > Is there simple way to do it ? > like fprintf in C/Perl world ? > > Thanks. > -Dmitry. > _______________________________________________ > Seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside > Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Yanni Chiu
Thanks,
this one looks practical. I am sad there is no Money in Squeak though... Yanni Chiu wrote: > > > In a 3.8 derived image, I get: > > 12.0 printShowingDecimalPlaces: 2. ==> '12.00' > 123.3453 printShowingDecimalPlaces: 2. ==> '123.35' > _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
in Wed, Feb 01, 2006 at 02:51:42PM +0300, Dmitry Dorofeev wrote:
> Thanks, > > this one looks practical. > I am sad there is no Money in Squeak though... Don't be sad, there is ScaledDecimal in Squeak :) Float is *not* appropriate for financial calculations, but ScaledDecimal should be OK. And #printShowingDecimalPlaces: will work with ScaledDecimal also: 123.3453 asScaledDecimal printShowingDecimalPlaces: 2. ==> '123.35' Dave _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
Thanks for tip, it is very helpful for a newbie like me.
Also, isn't it boring to see a list of suggestions for selector name when you type in your implementation of renderContentOn: something like self renderConfirmationOn: html. and save changes ? I'd like Squeak to create a missed selector for me and open it for edit (if save successful). Now I just accept my choice (click on renderConfirmationOn: in popup menu) select 'renderConfirmationOn: html' with a mouse + Alt+C click, Alt+V and only after that I can edit selector body. I hope the practical benefit of this feature is enough to excite someone :-) -Thanks, Dmitry David T. Lewis wrote: > in Wed, Feb 01, 2006 at 02:51:42PM +0300, Dmitry Dorofeev wrote: > >>Thanks, >> >>this one looks practical. >>I am sad there is no Money in Squeak though... > > > Don't be sad, there is ScaledDecimal in Squeak :) > > Float is *not* appropriate for financial calculations, but > ScaledDecimal should be OK. And #printShowingDecimalPlaces: will > work with ScaledDecimal also: > > 123.3453 asScaledDecimal printShowingDecimalPlaces: 2. ==> '123.35' > > Dave > > _______________________________________________ > Seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by David T. Lewis
Thanks for tip, it is very helpful for a newbie like me.
Also, isn't it boring to see a list of suggestions for selector name when you type in your implementation of renderContentOn: something like self renderConfirmationOn: html. and save changes ? I'd like Squeak to create a missed selector for me and open it for edit (if save successful). Now I just accept my choice (click on renderConfirmationOn: in popup menu) select 'renderConfirmationOn: html' with a mouse + Alt+C click, Alt+V and only after that I can edit selector body. I hope the practical benefit of this feature is enough to excite someone :-) -Thanks, Dmitry David T. Lewis wrote: > in Wed, Feb 01, 2006 at 02:51:42PM +0300, Dmitry Dorofeev wrote: > >>Thanks, >> >>this one looks practical. >>I am sad there is no Money in Squeak though... > > > Don't be sad, there is ScaledDecimal in Squeak :) > > Float is *not* appropriate for financial calculations, but > ScaledDecimal should be OK. And #printShowingDecimalPlaces: will > work with ScaledDecimal also: > > 123.3453 asScaledDecimal printShowingDecimalPlaces: 2. ==> '123.35' > > Dave > > _______________________________________________ > Seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by David T. Lewis
Hi,
Assuming I have prices with 2 digits after dot I want to calculate VAT with 4 digits precision. VAT May be shown still rounded to 2 digits, but some clients want to see even 4 digits after dot !!! With high turn over made from small sells it is some real money due to rounding errors :-) I made some tests and found a funny example: a := ScaledDecimal newFromNumber: 6.18 scale: 4. b := ScaledDecimal newFromNumber: 5 scale: 4. a / b 1.2359s4 a / b printShowingDecimalPlaces: 4 '1.2360' 6.18 / 5.0 1.236 why a / b shows 2.2359s4 ? but (a / b printShowingDecimalPlaces: 4) shows correct answer It makes me nervious.... Thanks. David T. Lewis wrote: > in Wed, Feb 01, 2006 at 02:51:42PM +0300, Dmitry Dorofeev wrote: > >>Thanks, >> >>this one looks practical. >>I am sad there is no Money in Squeak though... > > > Don't be sad, there is ScaledDecimal in Squeak :) > > Float is *not* appropriate for financial calculations, but > ScaledDecimal should be OK. And #printShowingDecimalPlaces: will > work with ScaledDecimal also: > > 123.3453 asScaledDecimal printShowingDecimalPlaces: 2. ==> '123.35' > > Dave > > _______________________________________________ > Seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
Hi,
I guess this is how ScaleDecimal prints itself. See printOn: in ScaleDecimal >> instance. BTW, if u comment last 2 lines in that method, you get the desired effect. Best regards, Valdas Bucinskas -----Original Message----- From: [hidden email] [mailto:[hidden email]]On Behalf Of Dmitry Dorofeev Sent: Thursday, February 02, 2006 4:14 PM To: The Squeak Enterprise Aubergines Server - general discussion. Subject: [Seaside] Money ScaledDecimal is it really *appropriate* ? Hi, Assuming I have prices with 2 digits after dot I want to calculate VAT with 4 digits precision. VAT May be shown still rounded to 2 digits, but some clients want to see even 4 digits after dot !!! With high turn over made from small sells it is some real money due to rounding errors :-) I made some tests and found a funny example: a := ScaledDecimal newFromNumber: 6.18 scale: 4. b := ScaledDecimal newFromNumber: 5 scale: 4. a / b 1.2359s4 a / b printShowingDecimalPlaces: 4 '1.2360' 6.18 / 5.0 1.236 why a / b shows 2.2359s4 ? but (a / b printShowingDecimalPlaces: 4) shows correct answer It makes me nervious.... Thanks. David T. Lewis wrote: > in Wed, Feb 01, 2006 at 02:51:42PM +0300, Dmitry Dorofeev wrote: > >>Thanks, >> >>this one looks practical. >>I am sad there is no Money in Squeak though... > > > Don't be sad, there is ScaledDecimal in Squeak :) > > Float is *not* appropriate for financial calculations, but > ScaledDecimal should be OK. And #printShowingDecimalPlaces: will > work with ScaledDecimal also: > > 123.3453 asScaledDecimal printShowingDecimalPlaces: 2. ==> '123.35' > > Dave > > _______________________________________________ > Seaside mailing list > [hidden email] > http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Dmitry Dorofeev
Yes, there's a bug when converting Floats to Scaled Decimals. It's
not precise. This works: a := ScaledDecimal newFromNumber: 618/100 scale: 4. b := ScaledDecimal newFromNumber: 5 scale: 4. a / b "1.2360s4" This is the problem: 6.18 asFraction (869757678035927/140737488355328) instead of (6.18 * 10e4) rounded / 10e4 (309/50) So when converting from a Float it should be rounded to scale, IMHO. You should report this (best along with the fix) on the squeak bug tracker. And because it's not a Seaside problem, we should continue the discussion to squeak-dev. - Bert - Am 02.02.2006 um 15:13 schrieb Dmitry Dorofeev: > Hi, > > Assuming I have prices with 2 digits after dot I want to calculate VAT > with 4 digits precision. VAT May be shown still rounded to 2 digits, > but some clients want to see even 4 digits after dot !!! With high > turn over made from small sells it is some real money due to > rounding errors :-) > > I made some tests and found a funny example: > > a := ScaledDecimal newFromNumber: 6.18 scale: 4. > b := ScaledDecimal newFromNumber: 5 scale: 4. > a / b 1.2359s4 > a / b printShowingDecimalPlaces: 4 '1.2360' > 6.18 / 5.0 1.236 > > why a / b shows 2.2359s4 ? > but (a / b printShowingDecimalPlaces: 4) shows correct answer > It makes me nervious.... > > Thanks. > > David T. Lewis wrote: >> in Wed, Feb 01, 2006 at 02:51:42PM +0300, Dmitry Dorofeev wrote: >>> Thanks, >>> >>> this one looks practical. >>> I am sad there is no Money in Squeak though... >> Don't be sad, there is ScaledDecimal in Squeak :) >> Float is *not* appropriate for financial calculations, but >> ScaledDecimal should be OK. And #printShowingDecimalPlaces: will >> work with ScaledDecimal also: >> 123.3453 asScaledDecimal printShowingDecimalPlaces: 2. ==> '123.35' >> Dave _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Dmitry Dorofeev
Dmitry Dorofeev wrote:
> Hi, > > Assuming I have prices with 2 digits after dot I want to calculate VAT > with 4 digits precision. VAT May be shown still rounded to 2 digits, > but some clients want to see even 4 digits after dot !!! With high > turn over made from small sells it is some real money due to rounding > errors :-) > > I made some tests and found a funny example: > > a := ScaledDecimal newFromNumber: 6.18 scale: 4. > b := ScaledDecimal newFromNumber: 5 scale: 4. > a / b 1.2359s4 > a / b printShowingDecimalPlaces: 4 '1.2360' > 6.18 / 5.0 1.236 > > why a / b shows 2.2359s4 ? > but (a / b printShowingDecimalPlaces: 4) shows correct answer > It makes me nervious.... > > Thanks. > represent money. If you need four digits then that dictates the scale of your integers. Maybe ScaledDecimals would do the job, don't know anything about them, but my experience dictates that it only takes 10 minutes to put together a Money (and/or Currency) class based on this integer representation and it is well worth the effort. Attached is one that I've used in the past...maybe not as robust as you need but you could probably hack it into your 4-digit form faster than you could learn about ScaledDecimal... Maybe somewants to post a more industry hardened version :-) David 'From Squeak3.7 of ''4 September 2004'' [latest update: #5989] on 2 February 2006 at 12:26:34 pm'! Magnitude subclass: #Money instanceVariableNames: 'cents' classVariableNames: '' poolDictionaries: '' category: 'Grocery-Domain'! !Money methodsFor: 'accessing' stamp: 'cds 3/27/2005 13:31'! cents ^cents! ! !Money methodsFor: 'comparing' stamp: 'cds 3/27/2005 13:35'! < other ^self cents < other cents! ! !Money methodsFor: 'comparing' stamp: 'cds 2/2/2006 12:26'! = other ^self species = other species and: [self cents = other cents]! ! !Money methodsFor: 'comparing' stamp: 'cds 3/27/2005 13:35'! hash ^self cents hash! ! !Money methodsFor: 'arithmetic' stamp: 'cds 3/27/2005 13:36'! + other ^Money cents: self cents + other cents! ! !Money methodsFor: 'initialize-release' stamp: 'cds 3/27/2005 13:31'! cents: anObject cents := anObject! ! !Money methodsFor: 'printing' stamp: 'cds 3/27/2005 19:03'! printOn: aStream aStream nextPutAll: '$'. self cents // 100 printOn: aStream. aStream nextPut: $.. self cents \\ 100 < 10 ifTrue: [aStream nextPut: $0]. cents \\ 100 printOn: aStream.! ! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! Money class instanceVariableNames: ''! !Money class methodsFor: 'instance creation' stamp: 'cds 3/27/2005 13:30'! cents: anInteger ^self new cents: anInteger; yourself! ! !Money class methodsFor: 'instance creation' stamp: 'cds 4/4/2005 12:49'! zero ^self cents: 0! ! 'From Squeak3.7 of ''4 September 2004'' [latest update: #5989] on 2 February 2006 at 12:26:48 pm'! !Integer methodsFor: '*Grocery' stamp: 'cds 3/27/2005 14:04'! cents ^self asMoney! ! 'From Squeak3.7 of ''4 September 2004'' [latest update: #5989] on 2 February 2006 at 12:28:26 pm'! TestCase subclass: #MoneyTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Grocery-Tests'! !MoneyTest methodsFor: 'tests' stamp: 'cds 3/27/2005 14:06'! testAddition | m1 m2 | m1 := 532 cents. m2 := 654 cents. self assert: (m1 + m2) cents = (532 + 654)! ! !MoneyTest methodsFor: 'tests' stamp: 'cds 3/27/2005 14:04'! testCreation | m | m := 100 asMoney. self assert: m cents = 100. m := 100 asDollars. self assert: m cents = 10000. m := Money cents: 10. self assert: m cents = 10. m := 965 cents. self assert: m cents = 965! ! !MoneyTest methodsFor: 'tests' stamp: 'cds 3/27/2005 19:02'! testPrinting self assert: 965 cents printString = '$9.65'. self assert: 430 cents printString = '$4.30'. self assert: 502 cents printString = '$5.02'! ! _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
On Feb 2, 2006, at 9:29 AM, David Shaffer wrote: > > I'd like to second what I believe Colin said about using integers to > represent money. If you need four digits then that dictates the scale > of your integers. Maybe ScaledDecimals would do the job, don't know > anything about them, but my experience dictates that it only takes 10 > minutes to put together a Money (and/or Currency) class based on this > integer representation and it is well worth the effort. Attached > is one > that I've used in the past...maybe not as robust as you need but you > could probably hack it into your 4-digit form faster than you could > learn about ScaledDecimal... > > Maybe somewants to post a more industry hardened version :-) I wouldn't call it industry hardened, but I've posted what we use in Dabble to http://squeaksource.com/Money . It uses the MoneyBag approach to dealing with currency that I remember seeing as a pattern somewhere... Avi _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
In reply to this post by Dmitry Dorofeev
> I'd like to second what I believe Colin said about using
> integers to represent money. If you need four digits then > that dictates the scale of your integers. Maybe > ScaledDecimals would do the job, don't know anything about > them, but my experience dictates that it only takes 10 > minutes to put together a Money (and/or Currency) class based > on this integer representation and it is well worth the > effort. Attached is one that I've used in the past...maybe > not as robust as you need but you could probably hack it into > your 4-digit form faster than you could learn about ScaledDecimal... > > Maybe somewants to post a more industry hardened version :-) > > David Heh, I wondered how long it'd be before someone said just use a Money object, this is Smalltalk after all. _______________________________________________ Seaside mailing list [hidden email] http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside |
Free forum by Nabble | Edit this page |