In England and Wales, this is handled by the wording of the Criminal Justice Act 2003, specifically section 240A, and it does not apply to all types of bail – only those with qualifying conditions and monitoring count for credit toward a sentence:
Time remanded on bail to count towards time served: terms of imprisonment and detention
(1) This section applies where—
(a) a court sentences an offender to imprisonment for a term in respect of an offence,
(b) the offender was remanded on bail by a court in course of or in connection with proceedings for the offence, or any related offence, after the coming into force of section 21 of the Criminal Justice and Immigration Act 2008, and
(c) the offender's bail was subject to a qualifying curfew condition and an electronic monitoring condition (“the relevant conditions”).
(2) Subject to F4 subsections (3A) and (3B), the court must direct that the credit period is to count as time served by the offender as part of the sentence.
Read the full law for more information, including the credits given, and also consider the Law Commissions guidance on sentencing in England and Wales page 126 for more clarity.