• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Scale a number based on its length  RSS feed

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
liliya woland
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,
I'm doing this in Jasper iReport in a place where a variable is expected, not a scriptlet.
 
Bartender
Posts: 9495
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping there is a cleaner way than a clunky loop.
 
Henry Wong
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
lowercase baba
Posts: 12628
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:

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


seems VERY clunky, to me.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 3171
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Marshal
Posts: 61774
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What’t wrong with working out the log10 of the number?
 
Mike Simmons
Ranch Hand
Posts: 3171
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, nothing? Which is why I suggested it.
 
Mike Simmons
Ranch Hand
Posts: 3171
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3171
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 61774
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!