Formula px to dp, dp to px android

I am trying to calculate a variable amount of pixels to density independent pixels and vice-versa. This formula (px to dp): dp = (int)(px / (displayMetrics.densityDpi / 160)); does not work on sm...