The fastest way to split uint64 into two others by digit position

71 views Asked by At

I have a function like:

func splitNumberByDecimals(u uint256, dec uint) (intFraction, decFraction uint){

 ...

 }

The task is to extract an unsigned number, and the number of decimal places for this number. And based on these decimals, split number into two others and return them.

Example:

num         = 123456
dec         = 100
intFraction = 123
decFraction = 456

It`s like divide a number by decimals and return the integer part and the remainder as uint:

123456/100 = 123,456

The problem is that I'm working with custom uint types whose size is 256bit. In addition, this function should be very lightweight. I can't use string casting or anything like that because it's very heavy. Is there some kind of bitwise algorithm that can be applied to this problem?

0

There are 0 answers