Converting unit abbreviations to numbers

So you want to translate SI unit abbreviations (‘K’,’M’,…) into exponents, and thus numerical powers-of-ten. Given that all units are single-letter, and the exponents are uniformly-spaced powers of 10**3, here’s working code that handles ‘Kilo’…’Yotta’, and any future exponents: > 10 ** (3*as.integer(regexpr(‘T’, ‘KMGTPEY’))) [1] 1e+12 Then just multiply that power-of-ten by the decimal value … Read more

Does setWidth(int pixels) use dip or px?

It uses pixels, but I’m sure you’re wondering how to use dips instead. The answer is in TypedValue.applyDimension(). Here’s an example of how to convert dips to px in code: // Converts 14 dip into its equivalent px Resources r = getResources(); int px = Math.round(TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));

Why em instead of px?

It is wrong to say that one is a better choice than the other (or both wouldn’t have been given their own purpose in the spec). It may even be worth noting that StackOverflow makes extensive use of px units. It is not the poor choice Spoike was told it was. Definition of units px … Read more