Home > Enterprise >  Remove trailing .0 from API returned `double`
Remove trailing .0 from API returned `double`

Time:02-01

I am calling an XML API that as part of its array returns a number, or a combination of letters and numbers, i.e. 702 or '''RA3'''.

It returns numbers, but adds .0 to the end, despite the call viewed in a web browser returning whole numbers such as 702, whilst my parse below returns 702.0

It also returns null to any that contain a letter, presumably this is because my method is a double, but I cant find a way around it.

My current method to parse it is as follows

class VehicleActivity {
  VehicleActivity({

    this.publishedLineName,

  double? publishedLineName;

        publishedLineName: 
            double.tryParse(vaElement.findAllElements('PublishedLineName').first.text)
...

I call the API in a separate method which works fine and then take the data with this

for (var vehicleActivity in vehicleActivity) {
var pln = vehicleActivity.publishedLineName.toString; //this is the problem line, I have tried all sorts of adaptations to it.
...

The actual API returns whole numbers, such as 702, but when I call this data from my method it returns 702.0. Also, any with letters are null, I guess because its a double.

I have tried every single method I can find online and none have worked. Thank you.

Update RH

I have looked into the XML returned and it returns values back such as...

lutter: 656.0
flutter: 53.0
flutter: null
2 flutter: 7.0
2 flutter: 702.0
flutter: 8.0
flutter: 3.0
flutter: 7.0

null being values such as RA1, which when I use to hashcode, 7.0 comes back as 7, but RA1 goes from null to 2011

I would like them be to returned without decimal places such as 7, 702, RA1, with letters included. These ideally would end up as a String, as thy are going into a List. But anyway to get the proper values back without null and .0 can be worked with. Thank you

CodePudding user response:

Change this:

double? publishedLineName;

        publishedLineName: 
            double.tryParse(vaElement.findAllElements('PublishedLineName').first.text)

to this:

String? publishedLineName;

        publishedLineName: 
            vaElement.findAllElements('PublishedLineName').first?.text
  •  Tags:  
  • Related