#1 2025-06-10 05:07:49

testgary
Member
Registered: 2025-02-06
Posts: 27

VariantToDouble

procedure TMvcApplication.RatingConvert(const Value: variant; out Result: variant);
var
  D: double = 0;
  B: boolean;
begin
  SetVariantNull(Result);

  // Normal
  OutputDebugString(PChar(VariantToString(Value)));
  VariantToDouble(variant(20.6170212765957), D); 
  D := Value;

  // Abnormal
  B := VariantToDouble(Value, D); 
end;

Value := 20.6170212765957

I don't know if it's my problem or someone else's problem

Last edited by testgary (2025-06-10 06:13:27)

Offline

#2 2025-06-10 07:20:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,069
Website

Re: VariantToDouble

Sorry, I don't get what is going on in your code, and what "abnormal" is?

Offline

#3 2025-06-10 10:09:40

testgary
Member
Registered: 2025-02-06
Posts: 27

Re: VariantToDouble

ab wrote:

Sorry, I don't get what is going on in your code, and what "abnormal" is?

It's not an error, but the data cannot be retrieved if the value is in a format like 20.6170212765957. This is because MongoDB's Double type stores numbers with such long decimal precision.

This is a method I defined using RegisterExpressionHelpers.

 (TMvcRunOnRestServer(fMainRunner).Views as TMvcViewsMustache).
  RegisterExpressionHelpers(['RatingConvert'], [@RatingConvert]); 

Offline

#4 2025-06-10 10:34:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,069
Website

Re: VariantToDouble

I still don't get it, sorry.

What is the BSON type?
betFloat or perhaps betDecimal128 ?

Offline

#5 2025-06-10 16:18:52

testgary
Member
Registered: 2025-02-06
Posts: 27

Re: VariantToDouble

procedure TMvcApplication.DoText(const Value: variant; out Result: variant);
var
  D: double = 0;
  B: Boolean;
begin
  // "D" does not receive a valid result, but integers do
  B := VariantToDouble(Value, D);
end;      

procedure TMvcApplication.Start(aRestModel: TRest; aInterface: PRttiInfo);
begin
  (TMvcRunOnRestServer(fMainRunner).Views as TMvcViewsMustache).
  RegisterExpressionHelpers(['DoText'], [@DoText]);  
end;  

// Frontend template snippet
{{DoText 20.6170212765957}}

Offline

#6 2025-06-10 16:49:53

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,069
Website

Re: VariantToDouble

I can't reproduce it.

Please debug a little to find out what is wrong.

Normally, TSynMustacheContext.ProcessHelper() should properly set the value using JsonToVariantInPlace(JSON_FAST_FLOAT).

Offline

Board footer

Powered by FluxBB