@@ -60,6 +60,8 @@ public static string ToLiteralString(object value, string? format) =>
6060 bool b => b . ToString ( ) ,
6161 int i => i . ToString ( ) ,
6262 long i => i . ToString ( ) ,
63+ float f => f . ToString ( ) ,
64+ double d => d . ToString ( ) ,
6365 string s => s ,
6466 Uri u => u . AbsoluteUri ,
6567 DateTimeOffset d => d . ToString ( "o" ) ,
@@ -92,10 +94,12 @@ public static BicepExpression ToBicep(object? value, string? format)
9294 null => BicepSyntax . Null ( ) ,
9395 bool b => BicepSyntax . Value ( b ) ,
9496 int i => BicepSyntax . Value ( i ) ,
95- // Note: below cast is valid because bicep limits to int.Min/MaxValue
96- // in bicep source and you need to use a parameter file for larger
97- // values
98- long i => BicepSyntax . Value ( ( int ) i ) ,
97+ long l => FromLong ( l ) ,
98+ // Note: bicep does not offically support floating numbers
99+ // therefore for floating numbers we are taking a workaround from
100+ // https://github.com/Azure/bicep/issues/1386#issuecomment-818077233
101+ float f => FromDouble ( f ) ,
102+ double d => FromDouble ( d ) ,
99103 string s => BicepSyntax . Value ( s ) ,
100104 Uri u => BicepSyntax . Value ( ToLiteralString ( u , format ) ) ,
101105 DateTimeOffset d => BicepSyntax . Value ( ToLiteralString ( d , format ) ) ,
@@ -121,6 +125,28 @@ IBicepValue v when (v.Kind == BicepValueKind.Unset) => BicepSyntax.Null(),
121125 _ => throw new InvalidOperationException ( $ "Cannot convert { value } to a Bicep expression.")
122126 } ;
123127
128+ BicepExpression FromLong ( long l )
129+ {
130+ // see if the value falls into the int range
131+ if ( l >= int . MinValue && l <= int . MaxValue )
132+ {
133+ return BicepSyntax . Value ( ( int ) l ) ;
134+ }
135+ // otherwise we use the workaround from https://github.com/Azure/bicep/issues/1386#issuecomment-818077233
136+ return BicepFunction . ParseJson ( BicepSyntax . Value ( l . ToString ( ) ) ) . Compile ( ) ;
137+ }
138+
139+ BicepExpression FromDouble ( double d )
140+ {
141+ // see if the value is a whole number
142+ if ( d >= int . MinValue && d <= int . MaxValue && d == Math . Floor ( d ) )
143+ {
144+ return BicepSyntax . Value ( ( int ) d ) ;
145+ }
146+ // otherwise we use the workaround from https://github.com/Azure/bicep/issues/1386#issuecomment-818077233
147+ return BicepFunction . ParseJson ( BicepSyntax . Value ( d . ToString ( ) ) ) . Compile ( ) ;
148+ }
149+
124150 ArrayExpression ToArray ( IEnumerable < object > seq ) =>
125151 BicepSyntax . Array ( [ .. seq . Select ( v => ToBicep ( v , v is BicepValue b ? b . Format : null ) ) ] ) ;
126152
0 commit comments