• Post Reply Bookmark Topic Watch Topic
  • New Topic

substring like function on BigDecimal/int datatype  RSS feed

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

What is the best way to apply a substring like function on java datatypes like int or say java object data type like BigDecimal.
Currently I am converting the same to String, getting the required values and then reconverting to int. It seems a bit sloppy to me.

So was wondering if this can be done in a more dexterous way.

Regards,
Mohit
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way to apply a substring like function on java datatypes like int or say java object data type like BigDecimal.


Well, "substring" is a string function, so technically there is no way to do it without it being a string.

However, if you want to simulate it -- meaning if you have converted it to a string, taken a substring, and then convert it back to an integer.... yes, the math is pretty straightforward. With a combination of modular (remainder) and substraction, you can easily extract the value, as it you had taken a substring.

It's actually pretty easy, so I'll let you take a shot at it first.

Henry
 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry for the response.
But I did not get it completely.
The situation I have will be something like this
BigDecimal value will be received as 20070531. We convert the same to int primitive data type. Now I want only the first 4 digits of the value which I can get it from the BigDecimal or convert to int and then grab the first 4 digits. How can I achieve the same.

-Mohit
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohit Sinha:
Thanks Henry for the response.
But I did not get it completely.
The situation I have will be something like this
BigDecimal value will be received as 20070531. We convert the same to int primitive data type. Now I want only the first 4 digits of the value which I can get it from the BigDecimal or convert to int and then grab the first 4 digits. How can I achieve the same.

-Mohit



It's basic mathematics.... But in order to understand it, you really need to attempt it yourself. Give it a try, it's not to hard to write a generic substring for an int.


On the other hand, if your example is the degree to what you want, then the answer is even easier...



Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I am at it, here is how you extract all three components...



As for a generic substring operation, I still leave that as an exercise -- it's not that hard... Try it yourself ...

Henry
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was pretty interesting to do. I'll post my solution tommorow in case he wants to give it a try.

Just something to look out for when you do your "substring" method on ints if you do substringInt(20070801, beginIndex, endIndex) and you start with leading zeros for the begin index ...I.E. (zero base indexes) 1,2,4,6 in our example they are gonna get chopped off...I spent about 15 minutes trying to debug (like an idiot) why substringInt(20070801, 1, 8) and substringInt(20070801, 3, 8) kept returning the same number: 70801
[ November 25, 2008: Message edited by: Paul Yule ]
 
Paul Yule
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
incase anyone wondered...

[ November 26, 2008: Message edited by: Paul Yule ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!