@@ -1072,6 +1072,57 @@ def test_formatting(self):
10721072 (',e' , '123456' , '1.23456e+5' ),
10731073 (',E' , '123456' , '1.23456E+5' ),
10741074
1075+ # negative zero: default behavior
1076+ ('.1f' , '-0' , '-0.0' ),
1077+ ('.1f' , '-.0' , '-0.0' ),
1078+ ('.1f' , '-.01' , '-0.0' ),
1079+
1080+ # negative zero: z option
1081+ ('z.1f' , '0.' , '0.0' ),
1082+ ('z6.1f' , '0.' , ' 0.0' ),
1083+ ('z6.1f' , '-1.' , ' -1.0' ),
1084+ ('z.1f' , '-0.' , '0.0' ),
1085+ ('z.1f' , '.01' , '0.0' ),
1086+ ('z.1f' , '-.01' , '0.0' ),
1087+ ('z.2f' , '0.' , '0.00' ),
1088+ ('z.2f' , '-0.' , '0.00' ),
1089+ ('z.2f' , '.001' , '0.00' ),
1090+ ('z.2f' , '-.001' , '0.00' ),
1091+
1092+ ('z.1e' , '0.' , '0.0e+1' ),
1093+ ('z.1e' , '-0.' , '0.0e+1' ),
1094+ ('z.1E' , '0.' , '0.0E+1' ),
1095+ ('z.1E' , '-0.' , '0.0E+1' ),
1096+
1097+ ('z.2e' , '-0.001' , '-1.00e-3' ), # tests for mishandled rounding
1098+ ('z.2g' , '-0.001' , '-0.001' ),
1099+ ('z.2%' , '-0.001' , '-0.10%' ),
1100+
1101+ ('zf' , '-0.0000' , '0.0000' ), # non-normalized form is preserved
1102+
1103+ ('z.1f' , '-00000.000001' , '0.0' ),
1104+ ('z.1f' , '-00000.' , '0.0' ),
1105+ ('z.1f' , '-.0000000000' , '0.0' ),
1106+
1107+ ('z.2f' , '-00000.000001' , '0.00' ),
1108+ ('z.2f' , '-00000.' , '0.00' ),
1109+ ('z.2f' , '-.0000000000' , '0.00' ),
1110+
1111+ ('z.1f' , '.09' , '0.1' ),
1112+ ('z.1f' , '-.09' , '-0.1' ),
1113+
1114+ (' z.0f' , '-0.' , ' 0' ),
1115+ ('+z.0f' , '-0.' , '+0' ),
1116+ ('-z.0f' , '-0.' , '0' ),
1117+ (' z.0f' , '-1.' , '-1' ),
1118+ ('+z.0f' , '-1.' , '-1' ),
1119+ ('-z.0f' , '-1.' , '-1' ),
1120+
1121+ ('z>6.1f' , '-0.' , 'zz-0.0' ),
1122+ ('z>z6.1f' , '-0.' , 'zzz0.0' ),
1123+ ('x>z6.1f' , '-0.' , 'xxx0.0' ),
1124+ ('🖤>z6.1f' , '-0.' , '🖤🖤🖤0.0' ), # multi-byte fill char
1125+
10751126 # issue 6850
10761127 ('a=-7.0' , '0.12345' , 'aaaa0.1' ),
10771128
@@ -1086,6 +1137,15 @@ def test_formatting(self):
10861137 # bytes format argument
10871138 self .assertRaises (TypeError , Decimal (1 ).__format__ , b'-020' )
10881139
1140+ def test_negative_zero_format_directed_rounding (self ):
1141+ with self .decimal .localcontext () as ctx :
1142+ ctx .rounding = ROUND_CEILING
1143+ self .assertEqual (format (self .decimal .Decimal ('-0.001' ), 'z.2f' ),
1144+ '0.00' )
1145+
1146+ def test_negative_zero_bad_format (self ):
1147+ self .assertRaises (ValueError , format , self .decimal .Decimal ('1.23' ), 'fz' )
1148+
10891149 def test_n_format (self ):
10901150 Decimal = self .decimal .Decimal
10911151
0 commit comments