Skip to main content
2 of 2
deleted 79 characters in body
Zombo
  • 1
  • 10

Rust, 124

fn f(n:f64)->String{
let n2=n.log(1e3);
let n3=n/1e3f64.powi(n2 as i32);
format!("{:.3}",n3)+["","k","M","G"][n2 as usize]
}
fn main(){
println!("{}",f(9012345678e0)=="9.012G");
}

Nim, 139

import math,strformat
proc f(n:float):string=
 let n2=int log10(n)/3
 let n3=n/pow(1e3,float n2)
 return fmt"{n3:.3f}"&["","k","M","G"][n2]
echo f(9012345678e0)=="9.012G"

Go, 145

package m
import(."fmt"
."math")
func f(g float64)string{h:=int(Log10(g))/3
i:=g/Pow10(h*3)
return Sprintf("%.3f",i)+[]string{"","k","M","G"}[h]}

D, 156

import std.format,std.math,std.stdio;
string f(real n){
auto n2=cast(int)log10(n)/3;
auto n3=n/pow(1e3,n2);
return format("%.3f",n3)~["","k","M","G"][n2];
}
void main(){
writeln(f(9012345678)=="9.012G");
}

C#, 168

using System;
class P{
static string f(double n){
var n2=(int)Math.Log10(n)/3;
var n3=n/Math.Pow(1e3,n2);
return String.Format("{0:f3}",n3)+new[]{"","k","M","G"}[n2];
}
static void Main(){
Console.WriteLine(f(9012345678)=="9.012G");
}
}
Zombo
  • 1
  • 10