Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22246 Provide #digitSum in Integer + tests #1628

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Kernel-Tests/IntegerTest.class.st
Expand Up @@ -457,6 +457,12 @@ IntegerTest >> testDifferentBases [
self assert: -36rZYXWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: -106300512100105327644605138221229898724869759421181854980.
]

{ #category : #'tests - other' }
IntegerTest >> testDigitSum [

self assert: 1234 digitSum equals: 10
]

{ #category : #'tests - basic' }
IntegerTest >> testDigitsAccess [

Expand Down
7 changes: 7 additions & 0 deletions src/Kernel/Integer.class.st
Expand Up @@ -951,6 +951,13 @@ Integer >> digitSubtract: arg [
^ sum normalize
]

{ #category : #accessing }
Integer >> digitSum [
"Returns the digit sum of the receiver"

^self asString inject: 0 into: [:value :new| value + new digitValue]
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returns bad values for negative numbers (like 8 for -9)


{ #category : #'truncation and round off' }
Integer >> floor [
"Refer to the comment in Number|floor."
Expand Down