Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format decimal places based on values?

How can I format a number in Excel so that if it's a whole number with a decimal, it goes to the 10th, but if it doesn't have a whole number it goes to 100th?

Example:

0.08 -> 0.08
0.02 -> 0.02
1.66 -> 1.6
2.55 -> 2.5
2.89 - > 2.8
3.66 -> 3.6
5 -> 5
8 -> 8

Edit:

So far the formula provided by JvdV is working

=IF(A1-INT(A1)>0.1,ROUNDDOWN(A1,1),A1)

but needs to adjust for the following:

1) I need anything with 0.1 to contain its original value. With the current formula above this changes 0.15 to 0.10.

Example:

0.11 -> 0.11
0.15 -> 0.15
0.16 -> 0.16
0.19 -> 0.19

2) The formula needs to round up .01 to .09 only when they have a whole number in front of them.

Example:

0.01 - > 0.01
0.07 -> 0.07
0.09 -> 0.09
0.99 -> 0.99
1 -> 1
1.01 -> 1.1
1.02 -> 1.1
1.04 -> 1.1
1.05 -> 1.1
1.06 -> 1.1
1.07 -> 1.1
1.08 -> 1.1
1.09 -> 1.1
2.01 -> 2.1
2.04 -> 2.1
3.03 -> 3.1
3.09 -> 3.1

This is tricky because 0.01 should be formatted as 0.01. 0.02 should be formatted as 0.02, etc. Currently, the formula does this but does not round up for whole numbers.

like image 703
a2b123 Avatar asked Dec 23 '22 21:12

a2b123


1 Answers

I guess something like this could work:

=IF(A1-INT(A1)>0.1,ROUNDDOWN(A1,1),A1)

After your edited question, my edited answer would look like:

=IF(A1>1,IF(A1-INT(A1)>0.1,ROUNDDOWN(A1,1),ROUNDUP(A1,1)),A1)
like image 140
JvdV Avatar answered Feb 04 '23 00:02

JvdV