#Convert to Integer

1 messages · Page 1 of 1 (latest)

quaint parrot
#

I want to convert string to integer (i32) and if error I want it to return 0 using function bellow:

fn convertToInteger(a: []const u8) i32 {
return std.fmt.parseInt(i32, a, 10);
}

but it always gives this error:
error: expected type 'i32', found 'error{InvalidCharacter,Overflow}!i32'
return std.fmt.parseInt(i32, a, 10);
~~~~~~^~

fossil inlet
#

std.fmt.parseInt returns an error union (that's what the message is saying btw)
What you need to do is to catch this error and return 0 in that case

quaint parrot
#

how can I catch that error union?

fossil inlet