It's always worth it to terraform low-hab planets into your ideal type, as the post above outlines. The more interesting question is, when is it worth it to terraform your habitability group into your ideal class.
Similar math, although I will go with the Shared Burdens approximation of 0.4 CG and 1 F baseline pop upkeep. In my experience this seems a fairly good approximation of a mixed economy on a planet, since it is likely that you wouldn't be running purely extraction jobs on a reasonable-habitability planet. The same conversion ratios shall be used as in the above post.
In the Early- to Midgame, an ideal world would have 20% more upkeep than the baseline, so 0.48 CG and 1.2 F. This would theoretically translate to 0.36 E upkeep per pop per month.
Same-class world would have 40% more upkeep than the baseline, at 0.56 CG and 1.4 F. This translates to 0.72 E upkeep per pop per month over the 100% case, and 0.36 E over the 80% case. This difference will stay throughout the game because habitability techs apply equally. Let's say you're aiming for a 120 pop world. Until it reaches capacity the math is a bit more more complicated and depends on the pop growth rate, so let's assume a growth rate of 4.5 per month, or 22 months per pop. In this case it takes 2596 months to fill the planet. In this timeframe the extra upkeep is essentially equal to 120*0.5*0.36*2596 = 56073 E. Obviously this is way higher than the meager cost of having terraformed to your ideal planet type. However, this is an unrealistic timeframe, because this is more than 200 years. Clearly, planets fill up faster than this, but this should give you a rough idea. [1]
This does not even begin to take into account that lower amenities either require you to invest more pops into amenity generation, or reduce production on the planet via slightly lower stability due to lower pop happiness.
To summarise: if you have the technology available, and you're sufficiently early in the game and have the bulk energy available, you should terraform any planet that isn't meant to be a small vital resource extraction operation. (And before anyone says anything about gene modding the habitability traits - currently not a viable option for any empire that does not run Migration Controls on their entire population)
[1] Edit: The more general formula can be expressed as PopGrowth * TimeToGrowToTargetPopulation * UpkeepPerPop * TimeToGrowToTargetPopulation * 0.5 = ExtraPopUpkeep. Obviously once you've reached the target population the (extra) upkeep levels out to being linear. Of note is that the extra pop upkeep until the planet is filled scales quadratically with the time to grow to said target population. So the sooner you cut it off, the sooner it levels off.
Second edit: I also tried writing the underlying math for determining the exact time left in the game until which a given planet is worth terraforming, but the generalized math gets rather messy before reaching the result, so I'll admit to being lazy and not finishing the job. For any sufficiently populous planet, i.e. a planet which has a sufficiently large target population - that is to say, one that will have surpassed the terraforming cost in extra upkeep while still growing, the formula for the "cutoff time" (time left in game until which it's worth terraforming) is, if I'm not much mistaken, as follows:
CutoffTime (in months) = TerraformationTime + SquareRoot( ( 2 * TerraformationCost ) / (PopGrowth * UpkeepPerPop) )
Of course, there is also the case to be made on filling up a planet via resettlement (slave population from other worlds, perhaps), in which case it is much much simpler:
CutoffTime (in months) = TerraformationTime + ( TerraformationCost / (TotalPops * UpkeepPerPop ) )
Note: UpkeepPerPop is the extra upkeep, not the total cost.