I need help to understand why is this happening?The scenario ,I just want to check if the set elements are all zero so I can exclude it from further processing.

List<Decimal> lstDecimal =new List<Decimal>{0.0,0.00};
Set<Decimal> setDecimal =new Set<Decimal>(lstDecimal);
system.debug('@NUR ..'+setDecimal+ ', '+setDecimal.size());

The output,the element only contains 0.0 but why the size is 2 which include 0.00.Should it printout something like this {0.0,0.00} ?

19:03:56:076 USER_DEBUG [3]|DEBUG|@NUR ..{0.0}, 2

With this, how I am going to implement checking for non zero elements set?

----------------------------Edited---------------------------------------------------------------------------------- I am looking for alternative checking non zero without using the loop because I have quite a lot of list to check ex. 1 segment can have many list of categories and if only all the categories list are zero I can exclude it from processing.Mean here one segment maybe contains 3 or 4 list of categories.So if I am going to iterate each set, I am afraid it will hiccup the performance.But I get solution something like this:

 Boolean allZero=true;
 List<Decimal> lstDecimal =new List<Decimal>{0.0,0.00,80.0};
 Set<Decimal> setToCheckZero=new Set<Decimal>(lstDecimal);

if(setToCheckZero.size()==1 && setToCheckZero.contains(0.0) ){
  //do nothing                                  


So by default it will assume the set contains zero data until it found one that contains data.So the program will process if isAllZero==false and ignore isAllZero==true.But I have concern with //do nothing portion.Is that okay to leave if portion without action because I don't want to change the value of isAllZero because it is true by default anyway.

Another alternative is:

  • The reply from Salesforce support:Set represents a collection of unique elements with no duplicate values. In your scenario, you have passed duplicate values in a list and then assigned that list to set. Set only returns unique value from the list. That is the reason why you are getting 0.0 as a result. size() returns the number of elements in the set (its cardinality). Since there are two values in the set, so it returns its size as 2. If there would be three values in the set, then the size will be returned as 3.
    – unidha
    Feb 6, 2015 at 6:07

1 Answer 1


This looks like its a bug with the toString method of Set in SalesForce. If you use the code below you will find that there is two values in the set and that both are correctly displayed when you iterate over the Set.

List<Decimal> lstDecimal = new List<Decimal>{0.0, 0.00};
Set<Decimal> setDecimal = new Set<Decimal>(lstDecimal);

System.debug('setDecimal: ' + setDecimal);
System.debug('setDecimal.size(): ' + setDecimal.size());
for (Decimal d : setDecimal) {
    system.debug('Value: ' + d);

This prints out:

DEBUG|setDecimal: {0.0}
DEBUG|setDecimal.size(): 2
DEBUG|Value: 0.0
DEBUG|Value: 0.00

You should raise a case with SalesForce to let them know that System.debug('setDecimal: ' + setDecimal); prints out a different result to whats in the Set

To answer your original question. If you want to check for non-zeros then you can do a few things but you would need to iterate over the elements to do it.

You could use setScale so that all elements have the same number of decimal places. You could use a lot of decimal places so that you don't lose any precision.


If you didn't want to include zeros in your list then you could do the comparison before adding it to the list:

if (myDecimal != 0) {

stripTrailingZeros is another method you could use to solve your problem.

  • Seems to be floating point precision issue.
    – ajinkyah
    Feb 4, 2015 at 12:08
  • I would expect that a set populated from a list of decimals {0.00, 0.0} would have only a single element - 0.0 so your results are interesting.
    – cropredy
    Feb 4, 2015 at 17:22
  • @crop1645 it is not only my result, you can try and it will return the same =) .It makes me pulling my hair before thinking is there something wrong my code etc.I am not sure how to raise case for this but I post the question here developer.salesforce.com/forums?state=id#!/…
    – unidha
    Feb 5, 2015 at 3:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .