Pada artikel ini, kita akan membahas tentang sebuah fungsi yang dapat mengubah format angka menjadi lebih mudah dibaca. Fungsi ini bernama "suffixNumber" dan dapat digunakan untuk mengubah angka besar menjadi format yang lebih mudah dipahami, seperti "milyar", "juta", atau "ribu". Dengan menggunakan fungsi ini, kita dapat dengan mudah memahami angka yang besar tanpa harus menghitung satu per satu.
Baiklah berikut inilah function-nya
function suffixNumber(number) { if (number >= 1000000000) { return (number / 1000000000).toFixed(1) + " milyar"; } else if (number >= 1000000) { return (number / 1000000).toFixed(1) + " juta"; } else if (number >= 1000) { return (number / 1000).toFixed(1) + " ribu"; } else { return number; } } console.log(suffixNumber(1000000)); // output: "1 juta" console.log(suffixNumber(10000000)); // output: "10 milyar" console.log(suffixNumber(1000)); // output: "1 ribu" console.log(suffixNumber(100)); // output: "100"
function suffixNumber($number) { if ($number >= 1000000000) { return number_format($number / 1000000000, 1) . " milyar"; } else if ($number >= 1000000) { return number_format($number / 1000000, 1) . " juta"; } else if ($number >= 1000) { return number_format($number / 1000, 1) . " ribu"; } else { return $number; } } echo suffixNumber(1000000); // output: "1 juta" echo suffixNumber(10000000); // output: "10 milyar" echo suffixNumber(1000); // output: "1 ribu" echo suffixNumber(100); // output: "100"
def suffixNumber(number): if number >= 1000000000: return format(number / 1000000000, '.1f') + " milyar" elif number >= 1000000: return format(number / 1000000, '.1f') + " juta" elif number >= 1000: return format(number / 1000, '.1f') + " ribu" else: return number print(suffixNumber(1000000)) # output: "1.0 juta" print(suffixNumber(10000000)) # output: "10.0 milyar" print(suffixNumber(1000)) # output: "1.0 ribu" print(suffixNumber(100)) # output: "100"
fun suffixNumber(number: Int): String { return when { number >= 1000000000 -> "${number / 1000000000} milyar" number >= 1000000 -> "${number / 1000000} juta" number >= 1000 -> "${number / 1000} ribu" else -> "$number" } } println(suffixNumber(1000000)) // output: "1 milyar" println(suffixNumber(10000000)) // output: "10 milyar" println(suffixNumber(1000)) // output: "1 ribu" println(suffixNumber(100)) // output: "100"
string suffixNumber(int number) { if (number >= 1000000000) { return (number / 1000000000).ToString("0.#") + " milyar"; } else if (number >= 1000000) { return (number / 1000000).ToString("0.#") + " juta"; } else if (number >= 1000) { return (number / 1000).ToString("0.#") + " ribu"; } else { return number.ToString(); } } Console.WriteLine(suffixNumber(1000000)); // output: "1 milyar" Console.WriteLine(suffixNumber(10000000)); // output: "10 milyar" Console.WriteLine(suffixNumber(1000)); // output: "1 ribu" Console.WriteLine(suffixNumber(100)); // output: "100"
string suffixNumber(int number) { if (number >= 1000000000) { return to_string((double)number / 1000000000).substr(0,4) + " milyar"; } else if (number >= 1000000) { return to_string((double)number / 1000000).substr(0,4) + " juta"; } else if (number >= 1000) { return to_string((double)number / 1000).substr(0,4) + " ribu"; } else { return to_string(number); } } cout << suffixNumber(1000000) << endl; // output: "1 milyar" cout << suffixNumber(10000000) << endl; // output: "10 milyar" cout << suffixNumber(1000) << endl; // output: "1 ribu" cout << suffixNumber(100) << endl; // output: "100"
def suffix_number(number) if number >= 1000000000 return (number / 1000000000.0).round(1).to_s + " milyar" elsif number >= 1000000 return (number / 1000000.0).round(1).to_s + " juta" elsif number >= 1000 return (number / 1000.0).round(1).to_s + " ribu" else return number.to_s end end puts suffix_number(1000000) # output: "1 milyar" puts suffix_number(10000000) # output: "10 milyar" puts suffix_number(1000) # output: "1 ribu" puts suffix_number(100) # output: "100"
func suffixNumber(number: Double) -> String { if number >= 1000000000 { return String(format: "%.1f milyar", number / 1000000000) } else if number >= 1000000 { return String(format: "%.1f juta", number / 1000000) } else if number >= 1000 { return String(format: "%.1f ribu", number / 1000) } else { return String(number) } } print(suffixNumber(number: 1000000)) // output: "1.0 juta" print(suffixNumber(number: 10000000)) // output: "10.0 milyar" print(suffixNumber(number: 1000)) // output: "1.0 ribu" print(suffixNumber(number: 100)) // output: "100.0"
package main import ( "fmt" ) func suffixNumber(number float64) string { if number >= 1e9 { return fmt.Sprintf("%.1f milyar", number/1e9) } else if number >= 1e6 { return fmt.Sprintf("%.1f juta", number/1e6) } else if number >= 1e3 { return fmt.Sprintf("%.1f ribu", number/1e3) } else { return fmt.Sprintf("%.1f", number) } } func main() { fmt.Println(suffixNumber(1000000)) // output: "1.0 juta" fmt.Println(suffixNumber(10000000)) // output: "10.0 milyar" fmt.Println(suffixNumber(1000)) // output: "1.0 ribu" fmt.Println(suffixNumber(100)) // output: "100.0" }
fn suffix_number(number: f64) -> String { if number >= 1_000_000_000.0 { format!("{:.1} milyar", number / 1_000_000_000.0) } else if number >= 1_000_000.0 { format!("{:.1} juta", number / 1_000_000.0) } else if number >= 1_000.0 { format!("{:.1} ribu", number / 1_000.0) } else { format!("{:.1}", number) } } fn main() { println!("{}", suffix_number(1000000.0)); // output: "1.0 juta" println!("{}", suffix_number(10000000.0)); // output: "10.0 milyar" println!("{}", suffix_number(1000.0)); // output: "1.0 ribu" println!("{}", suffix_number(100.0)); // output: "100.0" }
#include <stdio.h> #include <stdlib.h> #include <string.h> char* suffixNumber(unsigned long long number) { if (number >= 1000000000) { char *result = (char *) malloc(sizeof(char) * 20); sprintf(result, "%.1f milyar", (double) number / 1000000000); return result; } else if (number >= 1000000) { char *result = (char *) malloc(sizeof(char) * 20); sprintf(result, "%.1f juta", (double) number / 1000000); return result; } else if (number >= 1000) { char *result = (char *) malloc(sizeof(char) * 20); sprintf(result, "%.1f ribu", (double) number / 1000); return result; } else { char *result = (char *) malloc(sizeof(char) * 20); sprintf(result, "%llu", number); return result; } } int main() { printf("%s\n", suffixNumber(1000000)); // output: "1 juta" printf("%s\n", suffixNumber(10000000)); // output: "10 milyar" printf("%s\n", suffixNumber(1000)); // output: "1 ribu" printf("%s\n", suffixNumber(100)); // output: "100" return 0; }
sub suffixNumber { my $number = shift; if ($number >= 1000000000) { return sprintf("%.1f", $number / 1000000000) . " milyar"; } elsif ($number >= 1000000) { return sprintf("%.1f", $number / 1000000) . " juta"; } elsif ($number >= 1000) { return sprintf("%.1f", $number / 1000) . " ribu"; } else { return $number; } } print suffixNumber(1000000) . "\n"; # output: "1 juta" print suffixNumber(10000000) . "\n"; # output: "10 milyar" print suffixNumber(1000) . "\n"; # output: "1 ribu" print suffixNumber(100) . "\n"; # output: "100"
def suffixNumber(number: Double): String = { if (number >= 1000000000) { return f"${number / 1000000000}%.1f milyar" } else if (number >= 1000000) { return f"${number / 1000000}%.1f juta" } else if (number >= 1000) { return f"${number / 1000}%.1f ribu" } else { return number.toString } } println(suffixNumber(1000000)) // output: "1.0 juta" println(suffixNumber(10000000)) // output: "10.0 milyar" println(suffixNumber(1000)) // output: "1.0 ribu" println(suffixNumber(100)) // output: "100.0"
Itulah function suffixNumber() dalam beberapa bahasa pemrograman yang bisa kita terapkan sesuai kebutuhan. Mungkin cukup sekian saja, semoga bermanfaat.