public class String2Float {
final int max_exp = 38;
final int max_str_len = 20;
float [] values;
char [] buffer;
public String2Float() {
buffer = new char [max_str_len + 1];
values = new float [max_exp + 1];
values[ 0 ] = 1;
for( int i = 1; i < max_exp; i++ ) {
values[ i ] = values[ i - 1 ] * 10;
}
}
public float Value(String str) {
int len = str.length();
if( len > max_str_len )
len = max_str_len; // ToDo: add proper error handling
// extract all characters into the local buffer
str.getChars(0, len - 1, buffer, 0);
// find decimal point
int point_idx = len;
for( int i = 0; i < len; i++ ) {
if( buffer[i] == '.' ) {
point_idx = i;
break;
}
}
// process all digits before the decimal point
float res = 0;
for( int i = 0; i < point_idx ; i++ ) {
int digit = buffer[i] - '0';
float value = digit * values[point_idx - i - 1];
res += value;
}
// process all digits after the decimal point
for( int i = point_idx + 1; i < len; i++ ) {
int digit = buffer[i] - '0';
float value = digit / values[i - point_idx];
res += value;
}
return res;
}
}
On Apr 14, 5:02 pm, Mark Murphy <[email protected]> wrote:
> On Thu, Apr 14, 2011 at 7:47 PM, Paul <[email protected]> wrote:
> > OK, have stripped the FloatingPointParser bare, but can't run it as
> > the native method, parseFltImpl() is throwing an
> > UnsatisfiedLinkError...
>
> > Maybe a silly question, but any ideas what to do from here?
>
> Start from scratch, as Gaz Davidson did here:
>
> http://bitplane.net/2010/08/java-float-fast-parser/
>
> Or, as the last comment on that blog post indicates, grab the latest
> code from Harmony and try it.
>
> Or, roll your own String->Float using JNI and the NDK.
>
> --
> Mark Murphy (a Commons
> Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> Android Training in NYC:http://marakana.com/training/android/
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en