Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Scale a number based on its length Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scale a number based on its length" Watch "Scale a number based on its length" New topic
Author

Scale a number based on its length

liliya woland
Ranch Hand

Joined: Apr 11, 2006
Posts: 134
Hi all, I'm testing something and want to create a scale for number length representation. Say I have a number 4333, I want to take the number and say it's in a category 1000. Or the number is 324, the scale should be in 100. I want to do this inline - no loop, no var declaration. I have right now what's below, but it's far from the desired result. I know how to do this in a loop, but I want to have it inline. Seems like an interesting puzzle. If anyone can think of something, please let me know. Here is what I have:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18534
    
  40

liliya woland wrote:Hi all, I'm testing something and want to create a scale for number length representation. Say I have a number 4333, I want to take the number and say it's in a category 1000. Or the number is 324, the scale should be in 100. I want to do this inline - no loop, no var declaration. I have right now what's below, but it's far from the desired result. I know how to do this in a loop, but I want to have it inline. Seems like an interesting puzzle. If anyone can think of something, please let me know. Here is what I have:


May I ask for the reason of avoiding a loop? Is it purely as a puzzle?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
liliya woland
Ranch Hand

Joined: Apr 11, 2006
Posts: 134
Hi Henry,
I'm doing this in Jasper iReport in a place where a variable is expected, not a scriptlet.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

Do you mean, where a value is expected?

In that case, why don't you just put a method invocation there, and use a loop within the method body?
liliya woland
Ranch Hand

Joined: Apr 11, 2006
Posts: 134
I was hoping there is a cleaner way than a clunky loop.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18534
    
  40

liliya woland wrote:I was hoping there is a cleaner way than a clunky loop.


Well, you do know that your current solution also have a loop right ? ... it is just hidden in the Integer toString() method. If you implement a loop, that is specific to the task, clunky or not, it should be (unless you mess it up) more efficient than the loop that is designed for something else (to convert an int to a string).

Henry
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

This:
(Integer.toString(testNum).length()*10)

seems VERY clunky, to me.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

liliya woland wrote:If anyone can think of something, please let me know.

Well, BigDecimal.valueOf(testNum).precision() will return the length of an integer in decimal digits. Still pretty clunky, but maybe a bit clearer. Clearer still if you put it in a utility method (numDigits(int) ?).

However, if all you're trying to do is fit it in a predetermined space, what about using String.format()?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
fred rosenberger wrote:This:
(Integer.toString(testNum).length()*10)

seems VERY clunky, to me.

Also, it doesn't produce the desired results.

testNum = 4333 produces 40, should be 1000
testNum = 324 produces 30, should be 100

Liliya, I agree with others that the idea of avoiding a "clunky loop" is misguided. There are loops in most any library code you might call. You have a choice between writing the loop yourself, or calling code which does the loop for you. I like Winston's suggestion. Another approach is:

which is also clunky looking, but works.

Once again, looking at the source would reveal hidden loops. Oh well.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
What’t wrong with working out the log10 of the number?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Um, nothing? Which is why I suggested it.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Though I should have renamed numDigits as getNumberCategory or something similar, since I was returning what the poster originally asked for, which was not just the number length.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Mike Simmons wrote:Once again, looking at the source would reveal hidden loops.

Not with BigDecimal I don't think. For anything that fits in an int, I'm pretty certainly its just a multiplication and right-shift 31. But hey, what's a loop between friends?

Winston
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Hmmm, good point. Though I see more than one bit shift in BigDecimal.longDigitLength() (JDK 1.7 implementation) - still the whole thing probably adds up to less than two multiplications, so close enough.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Mike Simmons wrote:Um, nothing? Which is why I suggested it.
Damn! I hadn’t seen that post. I tend to open multiple tabs (since the previous/next buttons no longer exist), and posts can slip in which I don’t notice. I can have delays of over twenty minutes like that.

Sorry for missing that.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Scale a number based on its length
 
Similar Threads
cannot read double input from keyboard
function-array
unreachible code detected
Plz help! string class question
How to make a class smaller