Message Boards Message Boards


Round an integer to the two leading digits with trailing zeroes?

Posted 11 days ago
6 Replies
1 Total Likes

56789012 -> 57000000, for example. Thanks for any help.

6 Replies
rnd[n_] := Block[{d = 10^(Floor[Log[10, n]] - 1)}, d*Round[n/d]]


In[1]:= rnd[n_] := Round[n, 10^(Floor[Log[10, n]] - 1)]

In[2]:= rnd[56789012]

Out[2]= 57000000
Posted 10 days ago

The function Round could be used right out of the box. Documentation.

In[1]:= Round[56789012, 10^6]
Out[1]= 57000000

the second argument depends on the size of the number being rounded

Posted 10 days ago

Frank, you are right. The second argument of Round "out of the box" starts from the decimal point. But the OP wants a rounding starting from the leading digit. Which your function does.

Very helpful. Thank you, gentlemen.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract