1

I am using NCalc in my project to evaluate expressions. The framework includes a set of already implemented functions which can be found here.

I am interested in calculating the length of a number or a string. Can I achieve that using just the built-in functions?

Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
ArsenalRocks
  • 339
  • 1
  • 4
  • 12

1 Answers1

1

For getting length of an integer number, you can do something like this-

int length = ceiling(log10(number));

But a method like this will be much efficient-

int countLength(int number){
    if(number>9) return countLength(number/10) + 1;
    return 1;
}
Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64