From 90c55cd4dc785dc6a559722ae4f4d14eed51ae59 Mon Sep 17 00:00:00 2001 From: Bowero Date: Tue, 25 Jun 2019 10:04:02 +0200 Subject: [PATCH] Added different functions for casting to integers and strings --- builtin/string.v | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/builtin/string.v b/builtin/string.v index c33bd981a4..578fc4e3fc 100644 --- a/builtin/string.v +++ b/builtin/string.v @@ -138,16 +138,31 @@ pub fn (s string) replace(rep, with string) string { return tos(b, new_len) } -// TODO `.int()` ? -pub fn (s string) to_i() int { +// Casting to numbers (integers and floats) +fn (s string) to_i() int { return C.atoi(s.str) } -// TODO `.f32()` +fn (s string) int() int { + return s.to_i() +} + +fn (s string) i32() int { + return s.to_i() +} + fn (s string) to_float() float { return C.atof(s.str) } +fn (s string) float() float { + return s.to_float() +} + +fn (s string) f32() float { + return s.to_float() +} + // == fn (s string) eq(a string) bool { if isnil(s.str) {