Ruby, 8884
def f(n)
n2=Math.log10(n).to_i/3
return '%.3f'% (n/1e3**n2)+['',' k''k',' M''M',' G'][n2]'G'][n2]
end
p f(9012345678.0)=='9.012 G'012G'
Python, 9794
import math
def f(n):
n2=int(math.log10(n)/3)
return '%.3f'%(n/1e3**n2)+('',' k''k',' M''M',' G''G')[n2]
print(f(9012345678)=='9.012 G'012G')
JavaScript, 104
function f(n){
let n2=Math.trunc(Math.log10(n)/3);
return (n/1e3**n2).toFixed(3)+['','k','M','G'][n2];
}
console.log(f(9012345678)=='9.012G');
PHP, 100105
<?php
function f($n){
$n2=(int)(log10($n)/3);
return sprintf('%.3f',$n/1e3**$n2).['',' k''k',' M''M',' G'][$n2];'G'][$n2];
}
var_dump(f(9012345678)=='9.012 G'012G');
JavaScript, 108
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, 125124
fn f(n:f64)->String{
let n2=n.log(1e3);
let n=nn3=n/1e3f64.powi(n2 as i32);
format!("{:.3}",nn3)+[""," k""k"," M""M"," G"][n2"G"][n2 as usize]
}
fn main(){
println!("{}",f(9012345678e0)=="9.012 G"012G");
}
Nim, 140139
import math,strformat
proc f(n:float):string=
let n2=int log10(n)/3
let n=nn3=n/pow(1e3,float n2)
return fmt"{nn3:.3f}"&[""," k""k"," M""M"," G"][n2]"G"][n2]
echo f(9012345678e0)=="9.012 G"012G"
Dart, 143146
import 'dart:math';
String f(double n){
var n2=(log(n)/ln10/3).toInt();
nvar n3=n/=powpow(10001e3,n2);
return nn3.toStringAsFixed(3)+['',' k''k',' M''M',' G'][n2];'G'][n2];
}
void main(){
print(fvar s=f(9012345678)=='9;
print(s=='9.012 G'012G');
}
Go, 156
package main
import(
"fmt"
"math"
)
func f(n float64)string{
n2:=int(math.Log10(n))/3
n3:=n/math.Pow10(n2*3)
return fmt.Sprintf("%.3f",n3)+[]string{"","k","M","G"}[n2]
}
func main(){
println(f(9012345678)=="9.012G")
}
D, 151156
import std.format,std.math,std.stdio;
string f(real n){
auto n2=cast(int)log10(n)/3;
nauto n3=n/=powpow(1e3,n2);
return format("%.3f",nn3)~[""," k""k"," M""M"," G"][n2];"G"][n2];
}
void main(){
writeln(f(9012345678)=="9.012 G"012G");
}
C#, 153168
using System;
class AP{
static string Ff(double n){
intvar n2=Convert.ToInt32n2=(int)Math.Log10(n))/3;
nvar n3=n/=MathMath.Pow(1e3,n2);
return String.Format("{0:f3}",nn3)+new[]{""," k""k"," M""M"," G""G"}[n2];
}
static void Main(){
Console.WriteLine(Ff(9012345678)=="9.012 G"012G");
}
}
Go, 155
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")
}