## Ruby, 88 ~~~ruby def f(n) n2=Math.log10(n).to_i/3 return '%.3f'% (n/1000**n2)+['',' k',' M',' G'][n2] end puts f(9012345678.0)=='9.012 G' ~~~ ## Python, 97 ~~~py import math def f(n): n2=int(math.log10(n)/3) return '%.3f'%(n/1e3**n2)+('',' k',' M',' G')[n2] print(f(9012345678)=='9.012 G') ~~~ ## PHP, 100 ~~~php <?php function f($n){ $n2=(int)(log10($n)/3); return sprintf('%.3f',$n/1e3**$n2).['',' k',' M',' G'][$n2]; } var_dump(f(9012345678)=='9.012 G'); ~~~ ## JavaScript, 108 ~~~js function f(n){ let n2=Math.trunc(Math.log10(n)/3); return (n/1000**n2).toFixed(3)+['',' k',' M',' G'][n2]; } console.log(f(9012345678)=='9.012 G'); ~~~ ## Rust, 125 ~~~rust fn f(n:f64)->String{ let n2=n.log(1e3); let n=n/1e3f64.powi(n2 as i32); format!("{:.3}",n)+[""," k"," M"," G"][n2 as usize] } fn main(){ println!("{}",f(9012345678e0)=="9.012 G"); } ~~~ ## Nim, 140 ~~~py import math,strformat proc f(n:float):string= let n2=int log10(n)/3 let n=n/pow(1e3,float n2) return fmt"{n:.3f}"&[""," k"," M"," G"][n2] echo f(9012345678e0)=="9.012 G" ~~~ ## Dart, 143 ~~~dart import 'dart:math'; String f(double n){ var n2=(log(n)/ln10/3).toInt(); n/=pow(1000,n2); return n.toStringAsFixed(3)+['',' k',' M',' G'][n2]; } void main(){ print(f(9012345678)=='9.012 G'); } ~~~ ## D, 151 ~~~c import std.format,std.math,std.stdio; string f(real n){ auto n2=cast(int)log10(n)/3; n/=pow(1e3,n2); return format("%.3f",n)~[""," k"," M"," G"][n2]; } void main(){ writeln(f(9012345678)=="9.012 G"); } ~~~ ## C#, 153 ~~~cs using System; class A{ static string F(double n){ int n2=Convert.ToInt32(Math.Log10(n))/3; n/=Math.Pow(1e3,n2); return String.Format("{0:f3}",n)+new[]{""," k"," M"," G"}[n2]; } static void Main(){ Console.WriteLine(F(9012345678)=="9.012 G"); } } ~~~ ## Go, 155 ~~~go package main import( "fmt" "math" ) func f(n float64)string{ n2:=int(math.Log10(n))/3 n/=math.Pow10(n2*3) return fmt.Sprintf("%.3f",n)+[]string{""," k"," M"," G"}[n2] } func main(){ println(f(9012345678)=="9.012 G") } ~~~