Skip to main content
added 517 characters in body
Source Link
Gymhgy
  • 8k
  • 13
  • 35

C# (Visual C# Interactive Compiler), 283280 bytes

c=>{int d=c.IndexOf('.');int t=c.IndexOfAny(new char[]{'8','9','0','1'},1);var m=c[0]=='8'||c[0]=='9'?1>0:0>1;var z=decimal.Parse(c);Func<decimal>q=()=>(decimal)Math.Pow(10,c[0]=='8'||c[0]=='9'm?d<0?c.Length:d:d<0?c.Length-t:d>t?d-t:d-t+1);return c[0]=='8'||c[0]=='9'm?Math.Round(z/q())*q():t<0?z:Math.Round(z/q())*q();} 

Try it online!

It can be shorter if I used doubles instead of decimals, but I used decimals to preserve accuracy, or else a number like 547.4726 would be 547.472595214844.

Try it online!

C# (Visual C# Interactive Compiler), 268 bytes

c=>{int d=c.IndexOf('.');int t=c.IndexOfAny(new char[]{'8','9','0','1'},1);var m=c[0]=='8'||c[0]=='9'?1>0:0>1;var z=float.Parse(c);Func<double>q=()=>Math.Pow(10,m?d<0?c.Length:d:d<0?c.Length-t:d>t?d-t:d-t+1);return m?Math.Round(z/q())*q():t<0?z:Math.Round(z/q())*q();} 

Try it online!(Doubles Version, 271 bytes)Try it online!(Less accurate version)

C# (Visual C# Interactive Compiler), 283 bytes

c=>{int d=c.IndexOf('.');int t=c.IndexOfAny(new char[]{'8','9','0','1'},1);var z=decimal.Parse(c);Func<decimal>q=()=>(decimal)Math.Pow(10,c[0]=='8'||c[0]=='9'?d<0?c.Length:d:d<0?c.Length-t:d>t?d-t:d-t+1);return c[0]=='8'||c[0]=='9'?Math.Round(z/q())*q():t<0?z:Math.Round(z/q())*q();} 

It can be shorter if I used doubles instead of decimals, but I used decimals to preserve accuracy, or else a number like 547.4726 would be 547.472595214844.

Try it online!

Try it online!(Doubles Version, 271 bytes)

C# (Visual C# Interactive Compiler), 280 bytes

c=>{int d=c.IndexOf('.');int t=c.IndexOfAny(new char[]{'8','9','0','1'},1);var m=c[0]=='8'||c[0]=='9'?1>0:0>1;var z=decimal.Parse(c);Func<decimal>q=()=>(decimal)Math.Pow(10,m?d<0?c.Length:d:d<0?c.Length-t:d>t?d-t:d-t+1);return m?Math.Round(z/q())*q():t<0?z:Math.Round(z/q())*q();} 

Try it online!

It can be shorter if I used doubles instead of decimals, but I used decimals to preserve accuracy, or else a number like 547.4726 would be 547.472595214844.

C# (Visual C# Interactive Compiler), 268 bytes

c=>{int d=c.IndexOf('.');int t=c.IndexOfAny(new char[]{'8','9','0','1'},1);var m=c[0]=='8'||c[0]=='9'?1>0:0>1;var z=float.Parse(c);Func<double>q=()=>Math.Pow(10,m?d<0?c.Length:d:d<0?c.Length-t:d>t?d-t:d-t+1);return m?Math.Round(z/q())*q():t<0?z:Math.Round(z/q())*q();} 

Try it online!(Less accurate version)

Source Link
Gymhgy
  • 8k
  • 13
  • 35

C# (Visual C# Interactive Compiler), 283 bytes

c=>{int d=c.IndexOf('.');int t=c.IndexOfAny(new char[]{'8','9','0','1'},1);var z=decimal.Parse(c);Func<decimal>q=()=>(decimal)Math.Pow(10,c[0]=='8'||c[0]=='9'?d<0?c.Length:d:d<0?c.Length-t:d>t?d-t:d-t+1);return c[0]=='8'||c[0]=='9'?Math.Round(z/q())*q():t<0?z:Math.Round(z/q())*q();} 

It can be shorter if I used doubles instead of decimals, but I used decimals to preserve accuracy, or else a number like 547.4726 would be 547.472595214844.

Try it online!

Try it online!(Doubles Version, 271 bytes)