[Logo]
Forums Register Login
Scale a number based on its length
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:
 
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
Hi Henry,
I'm doing this in Jasper iReport in a place where a variable is expected, not a scriptlet.
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?
I was hoping there is a cleaner way than a clunky loop.
 
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
This:
(Integer.toString(testNum).length()*10)

seems VERY clunky, to me.
 
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
 
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.

What’t wrong with working out the log10 of the number?
Um, nothing? Which is why I suggested it.
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.
 
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
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.
 
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.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Oct 16, 2017 21:59:23.