Round number to nearest “nth” based on first non zero
问题 I want to round a Double to the nearest non zero number that follows the decimal. For example: x = 0.002341 rounded = 0.002 x = 0.000048123 rounded = 0.00005 For cases where the base number is > 0, it should perform as such x = 1.000234 rounded = 1.0002 I know I can use Double(round(1000*x)/1000) if I know the number of digits, but I want it to work for any number. Is there a swift function that does this? 回答1: You can have a little fun with logarithms to solve this: func roundFirst(x:Double)