Number.st
branchjv
changeset 18873 ce58d469e583
parent 18858 2968df243134
parent 18863 0fe8c5ecd2fd
child 19019 07dc163edbcc
--- a/Number.st	Wed Oct 28 10:00:01 2015 +0100
+++ b/Number.st	Thu Oct 29 06:54:02 2015 +0100
@@ -17,7 +17,8 @@
 
 ArithmeticValue subclass:#Number
 	instanceVariableNames:''
-	classVariableNames:'DecimalPointCharacterForPrinting DecimalPointCharactersForReading'
+	classVariableNames:'DecimalPointCharacterForPrinting DecimalPointCharactersForReading
+		DefaultDisplayRadix'
 	poolDictionaries:''
 	category:'Magnitude-Numbers'
 !
@@ -46,6 +47,13 @@
         DecimalPointCharacterForPrinting          <Character>                     used when printing
         DecimalPointCharactersForReading          <Collection of Character>       accepted as decimalPointChars when reading
 
+        DefaultDisplayRadix     the radix in which integers present their
+                                displayString (which is used in inspectors)
+                                If you are to look at many hex numbers, bitmasks
+                                etc. you may set this to 2 or 16.
+                                (avoids typing printStringRadix:.. all the time
+                                 - I know - I am lazy ;-). Default is 10.
+        
     [author:]
         Claus Gittinger
 
@@ -689,6 +697,21 @@
     "Modified: / 16.11.2001 / 14:13:16 / cg"
 ! !
 
+!Number class methodsFor:'misc'!
+
+displayRadix:aNumber
+    "being tired of always sending #printStringRadix: in the inspectors,
+     this allows you to change the default print radix for the displayString
+     method."
+
+    DefaultDisplayRadix := aNumber
+
+    "
+     Integer displayRadix:16. 123456 inspect
+     Integer displayRadix:10. 123456 inspect
+    "
+! !
+
 !Number class methodsFor:'private'!
 
 readMantissaAndScaleFrom:aStream radix:radix
@@ -1319,7 +1342,8 @@
 !
 
 ln
-    "compute ln of the receiver"
+    "return the natural logarithm of myself.
+     Raises an exception, if the receiver is less or equal to zero."
 
     (self isLimitedPrecisionReal not
     or:[self generality < 1.0 generality]) ifTrue:[
@@ -1446,6 +1470,63 @@
 
 !Number methodsFor:'printing & storing'!
 
+displayOn:aGCOrStream
+    "return a string to display the receiver.
+     The output radix is usually 10, but can be changed by setting
+     DefaultDisplayRadix (see Integer>>displayRadix:)"
+
+    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
+    "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
+    (aGCOrStream isStream) ifFalse:[
+        ^ super displayOn:aGCOrStream
+    ].
+
+    (DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[
+        self printOn:aGCOrStream
+    ] ifFalse:[
+        self printOn:aGCOrStream base:DefaultDisplayRadix showRadix:true.
+    ].
+
+    "
+     Integer displayRadix:16. 12345
+     Integer displayRadix:2.  12345
+     Integer displayRadix:10. 12345
+    "
+!
+
+printOn:aStream
+    "append a printed description of the receiver to aStream"
+
+    self printOn:aStream base:10
+
+    "Modified: / 20.1.1998 / 14:10:45 / stefan"
+!
+
+printOn:aStream base:b
+    "return a string representation of the receiver in the specified
+     radix (without the initial XXr)"
+
+    ^ self printOn:aStream base:b showRadix:false
+
+    "
+     10 printOn:Transcript base:3
+     31 printOn:Transcript base:3
+     -20 printOn:Transcript base:16
+     -20 printOn:Transcript base:10
+     3000 factorial printOn:Transcript base:10
+    "
+
+    "Modified: / 20.1.1998 / 18:05:02 / stefan"
+    "Modified: / 7.9.2001 / 13:52:17 / cg"
+!
+
+printOn:aStream base:b showRadix:showRadix
+    "the central print method for integer.
+     Must be defined in concrete classes"
+
+    self subclassResponsibility
+!
+
 printOn:aStream paddedWith:padCharacter to:size base:radix
     |s|
 
@@ -1540,6 +1621,21 @@
     "
 !
 
+printStringRadix:base showRadix:showRadixBoolean
+    "return a string representation of the receiver in the specified
+     base; does NOT prepend XXr to the string.
+     See also: radixPrintStringRadix:
+               printOn:base:showRadix:"
+
+    |s|
+
+    s := WriteStream on:(String basicNew:20).
+    self printOn:s base:base showRadix:showRadixBoolean.
+    ^ s contents
+
+    "Created: / 23-09-2011 / 13:59:19 / cg"
+!
+
 printStringWithThousandsSeparator
     "print the receiver as swiss business number with thousands separator to aStream.
      Caveat: Should use the separator from the locale here"
@@ -1604,6 +1700,25 @@
     "
 !
 
+radixPrintStringRadix:radix
+    "return a string representation of the receiver in the specified
+     base; prepend XXr to the string"
+
+    ^ self printStringRadix:radix showRadix:true
+
+    "
+     31 radixPrintStringRadix:2
+     31 radixPrintStringRadix:3
+     31 radixPrintStringRadix:10
+     31 radixPrintStringRadix:16
+     31 radixPrintStringRadix:36
+    "
+
+    "Created: / 19-01-1998 / 17:38:00 / stefan"
+    "Modified: / 20-01-1998 / 14:11:03 / stefan"
+    "Modified: / 23-09-2011 / 14:00:02 / cg"
+!
+
 storeOn:aStream
     "append a string for storing the receiver onto the argument, aStream
      - since numbers are literals,they store as they print."