Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.26k stars 4.61k forks source link

[QUERY] Why is there no AsDecimal/AsDouble for FieldValue? #20293

Closed yowl closed 2 years ago

yowl commented 3 years ago

Query/Question Looking at https://docs.microsoft.com/en-us/dotnet/api/azure.ai.formrecognizer.models.fieldvalue.ascountrycode?view=azure-dotnet-preview&viewFallbackFrom=azure-dotnet

If you have high precision numbers, and many forms require more than Single provides, why are there no methods for AsDouble or AsDecimal? I suppose the workaround is to leave as string type and do the conversion in userland.

Environment: -Azure.AI.FormRecognizer v3.1.0-beta.4

jsquire commented 3 years ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

AlexGhiondea commented 2 years ago

@kinelski @maririos is this something we should add?

Rodrick45 commented 2 years ago

AsDouble/AsDecimal methods won't do you any good if the data is stored internally as a float (the precision is already lost). I just want to make sure it is taken into account that the underlying datatype needs to change to solve this issue.

yowl commented 2 years ago

@Rodrick45 is this directed at me (author)? I'm working around this by using a string, so assume internally the data is there in its complete form as read from the OCR. E.g.

            var field = Form.Fields["allowedLaytime"];
            decimal d;
            if (decimal.TryParse(field.ValueData.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, ci, out d))
            {
Rodrick45 commented 2 years ago

I was directing my comment at whoever will be fixing this issue in the toolkit. Your workaround seems fine and is similar to what we had to do. Sorry for the confusion.

maririos commented 2 years ago

@Rodrick45 the service manages the numbers as double. The SDK is casting it as a float and then exposing it, so in the SDK we should definitely consider exposing AsDouble so there is no precision lost.

@kinelski @maririos is this something we should add?

@AlexGhiondea yes. We want to add it for future releases (4.0.0-beta.1). For other versions, we could consider leave as is as there is a workaround (like @yowl mentioned) or do a 3.2.0 GA release to include this.

kinelski commented 2 years ago

Update: the new DocumentField class introduced in SDK version 4.0.0-beta.1 contains an AsDouble method, as suggested. Here's a complete migration guide if there's any help needed in migrating to the new version: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/formrecognizer/Azure.AI.FormRecognizer/MigrationGuide.md

Closing this issue since it's been solved long ago. Feel free to open a new one if there are any other concerns.