Home > other >  Is there any possibility to cut the string to two first letters?
Is there any possibility to cut the string to two first letters?

Time:04-11

I parse Int object from API, but it's too long. So I want to cut it to two characters. I parse "6700000", but I want it to be "$6.7 m", or I parse "90000000", but I want to show "$90 m".

struct MillionView: View {
    @State var rockets = [RocketInfo]()
    
    var body: some View {
        List(rockets) { rocket in
            HStack {
                Text("Cost per launch")
                Spacer()
                Text("$\(rocket.costPerLaunch) m")
            }
        }
        .onAppear {
            InfoApi().getRockets { rockets in
                self.rockets = rockets
            }
        }
    }
}

struct MillionView_Previews: PreviewProvider {
    static var previews: some View {
        MillionView()
    }
}

CodePudding user response:

I was inattentive, but then I realized that the answer was:

Text("$\(rocket.costPerLaunch / 1000000) m")

I just divided the value by a million and got the desired result.

  • Related