In Spain, if you export energy, the cost is the balance between export/import by hour, if the positive, you pay, if it is negative, you get a discount.
Example:
Import price: 0.50
Export price: 0.40
If in one hour I import 1.5kwh and export 2kwh, the result is not 1.5*0.50 - 2 * 0.40 = -0.05, the result is 1.5 - 2 = -0.5 => -0.5 * 0.40 = -0.20