How to set DecimalSeparator for a float in C++ Builder - c++builder

I'm using C++ Builder and my locale is European, so I have a comma as a decimal separator.
I need to convert a double to a decimal point value with a DOT as separator.
I can't find an answer anywhere.

DecimalSeparator is a global variable, simply set it to the desired character before formatting the double, eg:
#include <SysUtils.hpp>
System::String FormatWithDot(double value)
{
System::Char old = Sysutils::DecimalSeparator;
Sysutils::DecimalSeparator = _D('.');
System::String s = Sysutils::FloatToStr(value);
Sysutils::DecimalSeparator = old;
return s;
}
System::String s = FormatWithDot(123.45);
Or, if you need to do this in multiple threads, use the thread-safe version, TFormatSettings::DecimalSeparator:
#include <SysUtils.hpp>
System::String FormatWithDot(double value)
{
Sysutils::TFormatSettings fmt = Sysutils::TFormatSettings::Create();
fmt.DecimalSeparator = _D('.');
return Sysutils::FloatToStr(value, fmt);
}
System::String s = FormatWithDot(123.45);
Just note that DecimalSeparator only applies to Delphi-based RTL functions like FloatToStr(), Format(), etc. It does not apply to C++-based functions like std::(s)printf(), std::to_string(), std::ostream::operator<<, etc. For those, you need to use C++ locales instead.

I solved it in the wee hours of the morning myself. You can use:
TFormatSettings fmt = TFormatSettings::Create();
fmt.DecimalSeparator = '.';
and then format a double like that:
FloatToStr(price, fmt);
I hope it helps someone. I was going crazy.

I know this this is a very old thread, but I was facing this problem today. In my case, my solution is this:
String DotFormatted(long double value, int numberOfDecimals=2) {
String result = Format("%." + IntToStr(numberOfDecimals) + "f", ARRAYOFCONST((value)));
result = StringReplace( result, ",", ".", TReplaceFlags() << rfReplaceAll);
return result;
}
It's equivalent to another one I had in a different language. For me, it's useful because of the numberOfDecimals argument.

Related

How to remove last element from a list in dart?

I'm a beginner in dart.
void main() {
var abf = '+37.4054-122.0999/';
var abf2;
abf2 = abf.replaceAll("+"," ");
var abf1 = abf2.split(RegExp('(?=[+-])'));
print (abf1[0]);
print (abf1[1]);
}
The above code splits abf into two values for me
I want to remove the ending '/'. I tried many split methods using other variables but it's not removing the '/' even though its removing the '+'.
It's not really clear what you're trying to do with the split.
But if you're looking the remove the / this should work:
String number = '+37.4054-122.0999/';
number = number.replaceAll("/"," ");
You can create substring from this while you like to remove last element.
String abf = '+37.4054-122.0999/';
final result = abf.substring(0, abf.length - 1);
print(result);
Dart's List class has a built-in removeLast method. Maybe you can try to split the string and then removing the last element:
String str = "str";
String newStr = str.split(''). removeLast().join('');

Systemverilog string variable as format specifier for $display/$write

I'm trying to create a variable format specifier for use in $display/$write. I've tried a large number of things, but here is what I have at the moment.
What I want to end up with is: $display(format_var,data_1,data_2), where the format string is pre-calculated using $sformatf or other.
Code:
module test;
function void pprint(input int data_1,input int field_1,input int data_2,input int field_2);
string format;
begin
format = $sformatf("%0d'h%%%0dx,%0d'h%%%0dx",field_1,field_1/4,field_2,field_2/4);
$display("format = %s",format);
$display(format,data_1,data_2);
end
endfunction
initial
begin
pprint(5,8,73737229,128);
$stop;
end
endmodule
The output I expect is:
format = 8'h%2x,128'h%32x
8'h05,128'h000000000000000000000000465240D
The output I get is:
format = 8'h%2x,128'h%32x
8'h%2x,128'h%32x 5 73737229
What do I need to do? The simulator is Vivado 2020.3
Later:
Trying more things, the following function does do what I want. My conclusion is that $display/$write can't take a variable as the format string, but $sformatf can.
function void pprint(input int data_1,input int field_1,input int data_2,input int field_2);
string format;
string outstr;
begin
format = $sformatf("%0d'h%%%0dx,%0d'h%%%0dx",field_1,field_1/4,field_2,field_2/4);
$display("format = %s",format);
$display("%s",$sformatf(format,data_1,data_2));
end
endfunction
Try:
function void pprint(
input logic [4095:0] data_1,
input int field_1,
input logic [4095:0] data_2,
input int field_2 );
string format;
format = $sformatf("%0d'h%%%0dh,%0d'h%%%0dh",
field_1, (field_1+3)/4,
field_2, (field_2+3)/4 );
$display("format = %s",format);
$display($sformatf(format,data_1,data_2));
endfunction
This should give you the output:
format = 8'h%02h,128'h%032h
8'h05,128'h000000000000000000000000465240D
Adding a zero between the % and digit could tells the simulator to pad the upper bits with zeros.
For some reason $display(format,data_1,data_2) did not use the format on simulators on edaplayground, but it did work with $sformatf so I simply nested it.
I needed to increase the bit width of the input data otherwise it would show leading zeros over 8 digits. Adjust if necessary.
Adding 3 to the field is for handling non multiples of 4. It will always round down after division.
According to section 21.3.3 Formatting data to a string of the SystemVerilog LRM, only $sformat and $sformatf have a specific formatting argument that can be a string literal or string variable. All other output tasks like $display treat any string literal argument as format specifiers and do not interpret the strings inside string variables for formatting.

How to convert ascii value in integer to its character equivalent in flutter?

I am new to flutter and I just want to display a list of alphabets in a for loop. I just want to know how can I convert the integer to ascii character. I searched for this and I found dart:convert library, but I don't know how to use it.
I want something like -
for(int i=65; i<=90; i++){
print(ascii(i)); //ascii is not any method, its just to understand my question
}
It should print the letters from 'A' to 'Z'.
You don't need dart:convert, you can just use String.fromCharCode
print(String.fromCharCode(i));
More info: https://api.dartlang.org/stable/2.0.0/dart-core/String/String.fromCharCode.html
In Dart, use these 2 functions to convert from int (byte) to String (char) and vice versa.
int value = ';'.codeUnitAt(0); //get unicode for semicolon
String char = String.fromCharCode(value); //get the semicolon string ;
This ia exactly what you need to generate your alphabet:
import 'dart:core';
void RandomString() {
List<int> a = new List<int>.generate(26, (int index) => index + 65);
String f = String.fromCharCodes(a);
print(f);
}
void main() {
RandomString();
}
Also You can copy, paste and test it here https://dartpad.dartlang.org/

Why str.FirstChar() does not return the first char?

UnicodeString us = "12345";
Label1->Caption= us.FirstChar();
The caption will show "12345" instead of "1".
Why is that?
The help page for FirstChar is empty:
Embarcadero Technologies does not currently have any additional
information. Please help us document this topic by using the
Discussion page!
The declaration is this:
const WideChar* FirstChar() const;
const WideChar* LastChar() const;
WideChar* FirstChar();
WideChar* LastChar();
The UnicodeString::FirstChar() method returns a pointer to the first character (just as the UnicodeString::LastChar() returns a pointer to the last character).
The data being pointed to is null-terminated. So the statement Label1->Caption = us.FirstChar(); is the same as if you had written Label1->Caption = L"12345"; instead. The TLabel::Caption property is also a UnicodeString, which has a constructor that accepts a null-terminated WideChar* pointer as input. That is why you see the result you are getting.
If you want just the first character by itself, use UnicodeString::operator[] instead:
Label1->Caption = us[1]; // UnicodeString is 1-indexed!
Or, using FirstChar(), simply dereference the pointer:
Label1->Caption = *(us.FirstChar());
Note that if the UnicodeString::IsEmpty() method returns true, both approaches will fail. operator[] will throw an ERangeError exception. FirstChar() will return a NULL pointer, which is undefined behavior to dereference. So watch out for that, eg:
if (!us.IsEmpty())
Label1->Caption = us[1];
else
Label1->Caption = _D("");
if (!us.IsEmpty())
Label1->Caption = *(us.FirstChar());
else
Label1->Caption = _D("");
A safer option would be to use the UnicodeString::SubString() method instead, which will return an empty string if the requested substring is out of range:
Label1->Caption = us.SubString(1, 1); // also 1-indexed!
Alternatively, you can use the RTL's System::Strutils::LeftStr() function instead:
#include <System.StrUtils.hpp>
Label1->Caption = LeftStr(us, 1);

In Dart is there a quick way to convert int to double?

Very simple issue. I have the useless class:
class Useless{
double field;
Useless(this.field);
}
I then commit the mortal sin and call new Useless(0);
In checked mode (which is how I run my tests) that blows up, because 'int' is not a subtype of type 'double'.
Now, it works if I use new Useless(0.0) , but honestly I spend a lot of time correcting my tests putting .0s everywhere and I feel pretty dumb doing that.
As a temporary measure I rewrote the constructor as:
class Useless{
double field;
Useless(num input){
field = input.toDouble();
}
}
But that's ugly and I am afraid slow if called often. Is there a better way to do this?
Simply toDouble()
Example:
int intVar = 5;
double doubleVar = intVar.toDouble();
Thanks to #jamesdlin who actually gave this answer in a comment to my previous answer...
In Dart 2.1, integer literals may be directly used where double is expected. (See https://github.com/dart-lang/sdk/issues/34355.)
Note that this is syntactic sugar and applies only to literals. int variables still won't be automatically promoted to double, so code like:
double reciprocal(double d) => 1 / d;
int x = 42;
reciprocal(x);
would fail, and you'd need to do:
reciprocal(x.toDouble());
You can also use:
int x = 15;
double y = x + .0;
use toDouble() method.
For e.g.:
int a = 10
print(a.toDouble)
//or store value in a variable and then use
double convertedValue = a.toDouble()
From this attempt:
class Useless{
double field;
Useless(num input){
field = input.toDouble();
}
}
You can use the parse method of the double class which takes in a string.
class Useless{
double field;
Useless(num input){
field = double.parse(input.toString()); //modified line
}
}
A more compact way of writing the above class using constructor's initialisers is:
class Useless{
double _field;
Useless(double field):_field=double.parse(field.toString());
}
Since all divisions in flutter result to a double, the easiest thing I did to achieve this was just to divide the integer value with 1:
i.e.
int x = 15;
double y = x /1;
There's no better way to do this than the options you included :(
I get bitten by this lots too, for some reason I don't get any warnings in the editor and it just fails at runtime; mighty annoying :(
I'm using a combination:
static double checkDouble(dynamic value) {
if (value is String) {
return double.parse(value);
} else if (value is int) {
return 0.0 + value;
} else {
return value;
}
}
This is how you can cast from int to double
int a = 2;
double b = a*1.0;

Resources