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.
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
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 Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
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'
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.
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
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 Seaside@lists.squeakfoundation.org 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 Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
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 Seaside@lists.squeakfoundation.org 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: seaside-bounces@lists.squeakfoundation.org [mailto:seaside-bounces@lists.squeakfoundation.org]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 Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
_______________________________________________ Seaside mailing list Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
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
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.
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
'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'! !
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
Thanks Avi,
I just wondering can I use Money amount: 15.89 currency: '£'
to use with HTML rendering (same applies to Euro sign)
or do we need just different method to rendering HTML friendly version of currency symbols based on name of currency ? like Dictionary with 'USD' -> $ 'GBP' -> '£' 'EUR' -> '€' etc
-Dmitry.
Avi Bryant wrote:
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 Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
On Feb 3, 2006, at 1:25 AM, Dmitry Dorofeev wrote:
Thanks Avi,
I just wondering can I use Money amount: 15.89 currency: '£'
to use with HTML rendering (same applies to Euro sign)
or do we need just different method to rendering HTML friendly version of currency symbols based on name of currency ? like Dictionary with 'USD' -> $ 'GBP' -> '£' 'EUR' -> '€'
The latter, I think.
We should probably have a Currency object, in fact, to encapsulate some of that logic. Not sure whether to have a subclass per currency or something simpler...
Avi
Avi Bryant wrote:
On Feb 3, 2006, at 1:25 AM, Dmitry Dorofeev wrote:
Thanks Avi,
I just wondering can I use Money amount: 15.89 currency: '£'
to use with HTML rendering (same applies to Euro sign)
or do we need just different method to rendering HTML friendly version of currency symbols based on name of currency ? like Dictionary with 'USD' -> $ 'GBP' -> '£' 'EUR' -> '€'
The latter, I think.
We should probably have a Currency object, in fact, to encapsulate some of that logic. Not sure whether to have a subclass per currency or something simpler...
Avi
Ok, here is the situation: I need currency support in Seaside or Squeak. But I am new to Smalltalk and have no idea what may be 'something simpler'. Could you please tell me some directions like 'subclass ABC and implement methods to convert USD to $ sign preferably named convertCode:'. If that gonna be part of Seaside, fine. If it is easy I will do my best to follow your directions and send back my code. (my first contribution!) BTW I am ready to consult standards bodies to fetch all currency symbols in UTF8 encoding! And all matched 3 letters currency codes. So probably it will be one class handling all associations for the planet ?
Thanks. -Dmitry
On 2/3/06, Dmitry Dorofeev dima-sender-3c337a@yasp.com wrote:
Ok, here is the situation: I need currency support in Seaside or Squeak.
Make a class Currency and a class Amount. Currency has: - description ("US Dollars") - ISO currency code ("USD") - common currency code ("$" or "US$") - code to print amounts. - number of decimals, if any.
Amount has: - a ScaledDecimal - a Currency - print code will be dispatched to Currency.
Bonus points for constructor methods in Number so you can write '12 dollars'. Double bonus points for hooking up to xe.com or similar so you can write '(12 dollars + 23 pounds) asEuros'. And finally triple bonus points for making amounts and currency conversions date-aware (I think I have some pointers to time travel patters in my blog).
Note that this quickly gets hairy and application specific. That, plus the fact that Squeak is not primarily a business-oriented development environment, is probably the cause that there's no standard package for this stuff.
Of course, by the time you're done, we'll welcome your publishing this on SqueakMap :)
2006/2/3, Cees De Groot cdegroot@gmail.com:
(I think I have some pointers to time travel patters in my blog).
Can you give me a link to them?
Regards,
-- Esteban A. Maringolo eMaringolo@gmail.com
http://www.manfred-lange.com/publications/TimeTravel.pdf
I think I have a Squeak implementation floating around somewhere of the Perspective/Edition stuff. With tests, even :)
On 2/3/06, Esteban A. Maringolo emaringolo@gmail.com wrote:
2006/2/3, Cees De Groot cdegroot@gmail.com:
(I think I have some pointers to time travel patters in my blog).
Can you give me a link to them?
Regards,
-- Esteban A. Maringolo eMaringolo@gmail.com _______________________________________________ Seaside mailing list Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Thanks Cees,
2006/2/3, Cees De Groot cdegroot@gmail.com:
I'm looking for a referenced paper: "[AJ97] Francis Anderson and Ralph Johnson, Tree with History" presented in PloP 97, if somebody has it or knows where I can found it, please contact me.
Thanks.
-- Esteban A. Maringolo eMaringolo@gmail.com
Esteban A. Maringolo wrote:
Thanks Cees,
2006/2/3, Cees De Groot cdegroot@gmail.com:
I'm looking for a referenced paper: "[AJ97] Francis Anderson and Ralph Johnson, Tree with History" presented in PloP 97, if somebody has it or knows where I can found it, please contact me.
Thanks.
According to Ralph Johnson, it appears that the referenced paper was submitted but not published as part of that conference. A revised and expanded version was presented as "A Collection of History Patterns" by Francis Anderson in the PLoP '98 conference. See
http://hillside.net/plop/plop98/final_submissions/
The PDF version of this new paper is available at
http://hillside.net/plop/plop98/final_submissions/P63.pdf
There are many other good papers available through
http://www.hillside.net/plop/pastconferences.html
-Mark Schwenk WellThot Inc. PLoP 2003 Conference Chair
Wonderful. I sign up to implement this. It will be a fun exercise. Should I create a project in SqueakSource called Currency? Avi would you prefer me to augment your Money package?
On 2/3/06, Cees De Groot cdegroot@gmail.com wrote:
http://www.manfred-lange.com/publications/TimeTravel.pdf
I think I have a Squeak implementation floating around somewhere of the Perspective/Edition stuff. With tests, even :)
On 2/3/06, Esteban A. Maringolo emaringolo@gmail.com wrote:
2006/2/3, Cees De Groot cdegroot@gmail.com:
(I think I have some pointers to time travel patters in my blog).
Can you give me a link to them?
Regards,
-- Esteban A. Maringolo eMaringolo@gmail.com _______________________________________________ Seaside mailing list Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
Seaside mailing list Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
-- Jason Rogers
"Where there is no vision, the people perish..." Proverbs 29:18
On Feb 3, 2006, at 10:11 AM, Jason Rogers wrote:
Wonderful. I sign up to implement this. It will be a fun exercise. Should I create a project in SqueakSource called Currency? Avi would you prefer me to augment your Money package?
Whatever's easiest for you - but even if it's a different package it should probably go into the same SqueakSource project. I'll open it up so anyone can write to it.
Avi
On 2/3/06, Avi Bryant avi.bryant@gmail.com wrote:
On Feb 3, 2006, at 10:11 AM, Jason Rogers wrote:
Wonderful. I sign up to implement this. It will be a fun exercise. Should I create a project in SqueakSource called Currency? Avi would you prefer me to augment your Money package?
Whatever's easiest for you - but even if it's a different package it should probably go into the same SqueakSource project. I'll open it up so anyone can write to it.
Avi
Thanks.
-- Jason Rogers
"Where there is no vision, the people perish..." Proverbs 29:18
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 Seaside@lists.squeakfoundation.org http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
seaside@lists.squeakfoundation.org